2014-10-06 58 views
0

我正在創建一個遊戲,並且我創建了一個移動到左右的橋。 我希望我的球員在與橋相撞時繼續前進。 我試圖設置位置球員的位置,但當我的球員在橋上他做小跳橋樑移動時移動播放器?

我該怎麼做?

我正在試着這個。

public class MoveBridge : MonoBehaviour { 

    private bool isLeft = false; 
    public float speed = 5f; 
    public float delaySpeed; 
    private float moveTime; 

    public GameObject player; 

    // Use this for initialization 
    void Start() { 

    } 

    // Update is called once per frame 
    void Update() { 
     move(); 
    } 

    private void move(){ 
     moveTime += Time.deltaTime; 

     if (moveTime <= delaySpeed){ 
      if (isLeft){ 
       gameObject.transform.Translate(-Vector2.right * speed * Time.deltaTime); 
      }else{     
       gameObject.transform.Translate(Vector2.right * speed * Time.deltaTime);     
      } 
     }else{ 
      isLeft = !isLeft; 
      moveTime = 0f; 
     } 
    } 


    void OnCollisionStay2D(Collision2D coll){  
     if(coll.gameObject.name.Equals("PlayerObject")){    
      player.transform.position = gameObject.transform.position; 
     }   
    } 

} 

回答

0

我解決了這個問題。

void OnCollisionStay2D(Collision2D coll){  
     if(coll.gameObject.name.Equals("PlayerObject")){    
      coll.gameObject.transform.parent = transform; 
     }   
    }