2016-02-01 113 views
0

當新場景被加載時,我的玩家出現在他處於上一級的位置。有什麼辦法可以爲此創造一個重生點?Unity DontDestroyOnload()

我使用了DoontDestoryOnLoad函數來讓玩家進入下一個場景,但是當玩家加載時,玩家將產生與他相同的x位置。

公共類DontDestroyPlz:MonoBehaviour {

// Use this for initialization 
void Awake() 
{ 
    DontDestroyOnLoad(this); 

    if (FindObjectsOfType(GetType()).Length > 1) 
    { 
     Destroy(gameObject); 
    } 
} 

// Update is called once per frame 
void Update() { 

} 

}

公共類MainMenu的:MonoBehaviour,IPointerEnterHandler,IPointerExitHandler {

void OnGUI() 
{ 


    if (GUI.Button(new Rect(Screen.width/1.5f, Screen.height/8, Screen.width/8, Screen.height/20), "Bunker")) 
    { 
     Application.LoadLevel("Bunker"); 
    } 


    if (GUI.Button(new Rect(Screen.width/3.5f, Screen.height/2.65f, Screen.width/8, Screen.height/20), "Forest")) 
    { 
     Application.LoadLevel("Forest"); 
    } 

    GUI.contentColor = Color.white; 
    GUI.backgroundColor = Color.magenta; 
    if (GUI.Button(new Rect(Screen.width/1.5f, Screen.height/1.5f, Screen.width/8, Screen.height/20), "Coming Soon!")) 
    { 
     Application.LoadLevel("Desert"); 
    } 

    GUI.contentColor = Color.white; 
    GUI.backgroundColor = Color.magenta; 
    if (GUI.Button(new Rect(Screen.width/3.0f, Screen.height/1.5f, Screen.width/8, Screen.height/20), "Out of Service")) 
    { 
     //Application.LoadLevel("Swamp"); 
    } 
} 

public void OnPointerEnter (PointerEventData data) 
{ 
    Debug.Log ("HERE BRUH!"); 
} 

public void OnPointerExit (PointerEventData data) 
{ 

} 

}

+0

的可能的複製[?如何重新啓動我的團結重裝現場評分(重置靜態分)](http://stackoverflow.com/questions/ 26934920 /我怎麼做我重新啓動我的分數重置靜態分數在重裝場景在統一) – Fattie

+0

這是一個億次重複 - 而一個好問題,最好回答有幫助的回答者簡單地鏈接到一個副本 – Fattie

+0

jeffrey - 我真的,真的,強烈建議你不要使用Unity的「非常古老」的gui系統。 (1)使用非常困難(2)普通系統Unity.UI的使用非常容易(我點擊'canvas'ii,點擊'添加按鈕'),(3)Unity很快將它移除,*項目將無法工作*。在主菜單腳本或dontdestroy腳本中,有無盡優秀的UI教程,https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button?playlist=17111 – Fattie

回答

2

使用方法OnLevelWasLoaded。每次加載新場景時都會調用它。例如,您可以predifine球員位置每一個場景:

Vector3 positionLevel1; 
Vector3 positionLevel2; 

void OnLevelWasLoaded(int level) 
{ 
    if (level == 1) 
     transform.position = positionLevel1; 
    else if (level == 2) 
     transform.position = positionLevel2; 
} 
+0

?或者把它放在播放器腳本中? – jeffery

+0

我會建議把它放在播放器上。 – Powderek

+0

非常感謝你! – jeffery