2016-08-15 13 views
0

私はUnityにはかなり新しいので、どうやってそれが最良の方法ではないかもしれないが、キャラクターがジャンプするとそれは矛盾する。プレーヤーのバウンスが矛盾しているのはなぜですか?

いくつかのバウンスは、他よりもさらにたくさんあると私はなぜこれが起こっているかわかりません。

また上向きの矢印を押しても実際には非常に速くなりますが、数秒待つと正常なように跳ね返ります。その代わりに力を追加するrigidbodys能力を利用して、オブジェクトを翻訳する

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

は、ここに私のコードです。私はまた、より単純な方法で衝突イベントを置き換えました。オブジェクト中心と地面との間の距離をチェックします。 (あなたはまだあなたの道をはっきりと使うことができます)。 FixedUpdateは、フレーム速度に依存するのではなく、コードが定期的に呼び出されることを保証します。あなたは再び


public float fspeed 

をジャンプすることができるまで、

//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 

(そのトピックHereの詳細は)地面に分の距離です前方や側速度(ジャンプです距離)


public float uspeed 

は、あなたの答えをneatenするには上向きの速度(高さをジャンプ)

+0

ですが、私は 'if'文の負荷を避けるために、あなたの' FixedUpdate() 'メソッド内で' switch'ステートメントを使用することをお勧め –

+0

バウンスは依然として矛盾しています。あなたが上向きの矢印を本当に速く押すと、プレーヤーのバウンスが長くなり始め、最終的には前方に滑り始めます。 –

関連する問題