2016-05-07 47 views
0

我有5個預製球員選項,在我的遊戲菜單上有一個「更改」按鈕,用戶點擊,改變角色。Instatiate預製Unity3D

如何在game2d啓動時應用「菜單」中的此選項?

組合屋:

蜜蜂,Bee1,Bee2,BEE3和Bee4。

蜜蜂(播放器)腳本。

using UnityEngine; 
using System.Collections; 
using UnityEngine.UI; 
using UnityStandardAssets.CrossPlatformInput; 

public class Bee : MonoBehaviour { 

    public float moveSpeed; 

    public Transform bee; 
    private Animator animator; 

    public bool isGrounded = true; 
    public float force; 

    public float jumpTime = 0.1f; 
    public float jumpDelay = 0.1f; 
    public bool jumped = false; 
    public Transform ground; 


    // Use this for initialization 
    void Start() 
    { 
     animator = bee.GetComponent<Animator>(); 
    } 

    void Update() 
    { 
     Move(); 

    } 


    void Move() 
    { 

     isGrounded = Physics2D.Linecast (this.transform.position, ground.position, 1 << LayerMask.NameToLayer ("Floor")); 
     animator.SetFloat ("runB", Mathf.Abs (CrossPlatformInputManager.GetAxis ("Horizontal"))); 
     if (CrossPlatformInputManager.GetAxisRaw ("Horizontal") > 0) { 

      transform.Translate (Vector2.right * moveSpeed * Time.deltaTime); 
      transform.eulerAngles = new Vector2 (0, 0); 
     } 
     if (CrossPlatformInputManager.GetAxisRaw ("Horizontal") < 0) { 

      transform.Translate (Vector2.right * moveSpeed * Time.deltaTime); 
      transform.eulerAngles = new Vector2 (0, 180); 
     } 

     if (CrossPlatformInputManager.GetButtonDown ("Vertical") && isGrounded && !jumped) { 

      // rigidbody2D.AddForce (transform.up * force); 
      GetComponent<Rigidbody2D>().AddForce (transform.up * force); 
      jumpTime = jumpDelay; 
      animator.SetTrigger ("jumpB"); 
      jumped = true; 
     } 

     jumpTime -= Time.deltaTime; 

     if (jumpTime <= 0 && isGrounded && jumped) { 

      animator.SetTrigger ("groundB"); 
      jumped = false; 

     } 

    } 

} 
+0

預製件之間的區別是什麼?例如。如果它只是在視覺效果上有所不同,那麼您應該使用單一的預製,並讓它的某個孩子擁有視覺效果(例如精靈),並且只能改變它。 –

+0

@Gunnar B.只更改精靈,其餘都一樣! –

回答

0

我推薦給玩家的視覺部分移動到子對象,這不是必需的雖然。如果你這樣做,你的根節點對象可以只是一個空的遊戲對象。

如果您有場景更改,您將需要在場景之間持續存在的某些內容,例如,與DontDestroyOnLoad單身。無論如何,這必須具有關於所有可能的精靈的信息,例如,在您填寫Resources.Load/LoadAll的列表中。您可以使用此列表在菜單中顯示精靈。在選擇保存指數,並在遊戲開始,你只需要改變玩家精靈到選定一個像這樣的東西(這將是在這個類中,如被稱爲GameController):

playerGO.GetComponentInChildren<SpriteRenderer>().sprite = spritesList[selected]; 

(這是假設視覺效果在兒童物體上。)