2016-09-14 32 views
0

CharacterControllerをPlayerに追加します。しかし、私がジャンプ関数をテストすると、Playerがすぐに上に移動することがわかります。Unity3Dでスムーズにジャンプする方法

if (Player.isGrounded) 
    { 
     if (jump) 
     { 
      Move.y = JumpSpeed; 
      jump = false; 
      Player.Move (Move * Time.deltaTime); 
     } 
    } 
    Move += Physics.gravity * Time.deltaTime * 4f; 
    Player.Move (Move * Time.fixedDeltaTime);` 
+0

より広いコードサンプルが役に立ちます。このスニペットはFixedUpdate()にありますか?プレーヤーのゲームオブジェクトにリジッドボディーが取り付けられていますか? – Augure

答えて

0
  1. あなたは1つのフレームで二回Player.Move()を呼んでいます。これは問題かもしれません。
  2. の重力をMoveベクトルに追加します。つまり、このコードを呼び出すと常に上に移動します。
  3. Moveのような変数に名前を付けるのは良い規則ではありません。同じ名前のメソッドがすでに存在するため、読み込み中に混乱が生じます。 moveDirectionに変更してください。ここで

サンプルコードです:

public class ExampleClass : MonoBehaviour { 
    public float speed = 6.0F; 
    public float jumpSpeed = 8.0F; 
    public float gravity = 20.0F; 
    private Vector3 moveDirection = Vector3.zero; 
    CharacterController controller; 
    void Start() 
    { 
     controller = GetComponent<CharacterController>(); 
    } 

    void Update() { 
     if (controller.isGrounded) { 
      moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); 
      moveDirection = transform.TransformDirection(moveDirection); 
      moveDirection *= speed; 
      if (Input.GetButton("Jump")) 
       moveDirection.y = jumpSpeed; 

     } 
     moveDirection.y -= gravity * Time.deltaTime; 
     controller.Move(moveDirection * Time.deltaTime); 
    } 
} 

は、このことができます願っています。

+0

ありがとう^。^、それは多くの助けになります – Saber

+0

答えを正確にマークしてください。 –

関連する問題