2016-08-15 20 views
0

我對Unity來說很新,所以我怎麼做可能不是最好的方法,但是當字符跳轉時它是不一致的。爲什麼玩家的反彈不一致?

有些反彈比其他反彈要多得多,我不確定爲什麼會發生這種情況。

另外,如果我真的很快按下向上箭頭,立方體跳得很遠,但如果等待幾秒鐘,它會像平常一樣反彈。

這裏是我的代碼:

using UnityEngine; 
using System.Collections; 

public class MovePlayer : MonoBehaviour 
{ 
    Vector3 endPos; 
    int numBackwards = 0; 
    bool jumping = false; 
    public Rigidbody rigidBody; 
    //public Collider theCollider; 

    void Start() 
    { 
     rigidBody = GetComponent<Rigidbody>(); 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     rigidBody.freezeRotation = true; 

     endPos = gameObject.transform.position; 
     if (!jumping) 
     { 
      if (Input.GetButtonDown("up") && gameObject.transform.position == endPos) { 
       if (numBackwards < 0) 
       { 
        numBackwards++; 
       } 
       else 
       { 
        UpdateScore.score++; 
       } 
       transform.Translate(Vector3.up * 50 * Time.deltaTime, Space.World); 
       transform.Translate(Vector3.forward * 110 * Time.deltaTime, Space.World); 
      } 
      else if (Input.GetButtonDown("down") && gameObject.transform.position == endPos) 
      { 
       transform.Translate(Vector3.up * 50 * Time.deltaTime, Space.World); 
       transform.Translate(-Vector3.forward * 110 * Time.deltaTime, Space.World); 
       numBackwards--; 
      } 
      else if (Input.GetButtonDown("left") && gameObject.transform.position == endPos) 
      { 
       transform.Translate(Vector3.up * 50 * Time.deltaTime, Space.World); 
       transform.Translate(Vector3.left * 110 * Time.deltaTime, Space.World); 
      } 
      else if (Input.GetButtonDown("right") && gameObject.transform.position == endPos) 
      { 
       transform.Translate(Vector3.up * 50 * Time.deltaTime, Space.World); 
       transform.Translate(Vector3.right * 110 * Time.deltaTime, Space.World); 
      } 
     } 
    } 

    void OnCollisionEnter(Collision other) 
    { 
     if (other.gameObject.CompareTag("Ground")) 
      jumping = false; 
    } 

    void OnCollisionExit(Collision other) 
    { 
     if (other.gameObject.CompareTag("Ground")) 
      jumping = true; 
    } 
} 

回答

1

相反平移對象,利用自己的一份力量添加到它的rigidbodys能力。我也用一種更簡單的方法取代了碰撞事件。它檢查物體中心和地面之間的距離。 (你仍然可以明顯地使用你的方式)。固定更新確保代碼將被定期調用,而不是取決於幀速度。 (有關該主題的Here更多)

//PUBLIC 
public float distance;  
public float fspeed; 
public float uspeed; 

//PRIVATE 
private Rigidbody rigidBody; 


void Awake() 
{ 
    rigidBody = GetComponent<Rigidbody>(); 
    rigidBody.freezeRotation = true; 
} 

void FixedUpdate() 
{ 
    if (Physics.Raycast(transform.position, -Vector3.up, distance + 0.1F)) 
    { 
     if (Input.GetKeyDown(KeyCode.UpArrow)) 
     { 
      rigidBody.AddForce(new Vector3(0, uspeed, fspeed)); 
     } 

     if (Input.GetKeyDown(KeyCode.DownArrow)) 
     { 
      rigidBody.AddForce(new Vector3(0, uspeed, -fspeed)); 
     } 

     if (Input.GetKeyDown(KeyCode.LeftArrow)) 
     { 
      rigidBody.AddForce(new Vector3(fspeed, uspeed, 0)); 
     } 

     if (Input.GetKeyDown(KeyCode.RightArrow)) 
     { 
      rigidBody.AddForce(new Vector3(-fspeed, uspeed, 0)); 
     } 
    } 
} 

public float distance 

是到地面的最小距離,直到你可以再次跳躍


public float fspeed 

是正向或側速度(跳躍距離)


public float uspeed 

是向上的速度(跳躍高度)

+0

要neaten你的答案,我會建議使用您的'FixedUpdate()'方法中的'之開關語句,避免負載'if'聲明 –

+0

反彈仍然不一致。當你快速按下向上箭頭時,球員反彈將開始變長,並最終開始向前滑動。 –

相關問題