2013-02-17 164 views
0

我試圖用最簡單的物理學(與重力墜落,左右移動,(也許)跳躍)製作最簡單的平臺遊戲原型。例如:無物理碰撞

void Move() { 
    if (!isDying || !isDead || !isShooting || !isFalling) 
    { 
    float amountToMove = Time.deltaTime * Input.GetAxis("Horizontal") * playerSpeed; 
    transform.Translate(Vector3.right * amountToMove); 
    } 
} 

void ApplyGravity() { 

    float amountToMove = Time.deltaTime * gravity; 
    transform.Translate(Vector3.down * amountToMove); 

} 

問題是我不知道如何使沒有物理和啓用isKinematic的碰撞。我唯一知道的是使用OnTriggerEnter功能,因此增加了isTrigger的所有對象,寫(Hero.cs):

void OnTriggerEnter (Collider otherGameObjectCollider) { 
    if (otherGameObjectCollider.gameObject.tag == "ground") { 
    Debug.Log("ground/wall collision"); 
    } 
} 

我知道我需要停止我的英雄,以防止墜落(步行)通過地面,但我真的不能想出來。

對不起愚蠢的問題。

回答

0

您可以使用英雄的isFalling旗幟來判斷他是否處於墜落狀態。試試這樣:

void ApplyGravity() { 
    if (isFalling) { 
    float amountToMove = Time.deltaTime * gravity; 
    transform.Translate(Vector3.down * amountToMove); 
    } 
} 

void OnTriggerEnter (Collider otherGameObjectCollider) { 
    if (otherGameObjectCollider.gameObject.tag == "ground") { 
    Debug.Log("ground/wall collision"); 
    isFalling = false; 
    } 
} 

而當你的英雄離開的平臺,而不是在一個跳躍,你應該設置isFalling爲true,使他開始下降。

也許你想試試看看Unity officail演示:3D Platformer Tutorial。我相信它包含了構建平臺遊戲所需的所有技巧和方法。祝你好運。 :)