2015-06-27 180 views
0

我正在製作2.5D plataform遊戲,但無法正確旋轉玩家。我只是希望它在X軸上旋轉,而玩家向左和向右移動。我的動作腳本是這樣的:在2D場景中旋轉3D對象

 if (isWalking) 
    { 
     transform.Rotate(0, facingDir, 0); 
     isWalking = false; 
    } 
    else { } 

    if (Input.GetKey(KeyCode.Space)) 
     GetComponent<Rigidbody>().velocity = new Vector2(GetComponent<Rigidbody>().velocity.x, jumpHeight); 

    if (Input.GetKey(KeyCode.A)){ 
     GetComponent<Rigidbody>().velocity = new Vector2(-speedHeight, GetComponent<Rigidbody>().velocity.y); 
     facingDir = 180; 
     isWalking = true; 
    } 

    if (Input.GetKey(KeyCode.D)) 
    { 
     GetComponent<Rigidbody>().velocity = new Vector2(speedHeight, GetComponent<Rigidbody>().velocity.y); 
     facingDir = 0 ; 
     isWalking = true; 
    } 

我可以旋轉它是由transform.rotate(0,180,0)和(0,0,0),但隨後繼續轉動不停的最好方法,怎麼可以告訴玩家在X軸上的移動方向,以便我可以正確旋轉。

回答

1
transform.forward 

這應該給各位玩家在移動的方向。您可以使用

transform.LookAt(target); 

以定向玩家在特定的方向。主要問題是您正在嘗試更新GetKey函數中的方向,該函數在按住按鍵的情況下返回true。這會導致角色不斷旋轉,您通過使用額外的檢查來避免這種情況。您可以使用Lookat代替以下方式:

GetComponent<Rigidbody>().velocity = new Vector2(-speedHeight, GetComponent<Rigidbody>().velocity.y); 

transform.LookAt(transform.position+new Vector3(GetComponent<Rigidbody>().velocity.x,0,GetComponent<Rigidbody>().velocity.y)); 

有更好的方法來做到這一點,但這應該可以解決您的問題。