2016-04-14 37 views
3

大家好!C#Unity字符跳躍真奇怪

我的UNITY 5代碼有問題。

我的角色可以跳躍,但他立即跳躍,速度太快。 它看起來很奇怪。

我將不勝感激您對我的代碼的反饋!

using UnityEngine; 
using System.Collections; 

public class Gravity : MonoBehaviour { 
    private float inputDirection; 
    private float VerticalSpeed; 
    private float gravity = -2f; 
    private float speedmultiplier = 5f; 
    private bool jump; 

    private Vector3 moveVector; 
    private CharacterController controller; 

    // Use this for initialization 
    void Start() { 
     controller = GetComponent<CharacterController>(); 
    } 

    // Update is called once per frame 
    void Update() { 
     inputDirection = Input.GetAxis ("Horizontal"); 
     moveVector = new Vector3 (inputDirection, VerticalSpeed, 0) * speedmultiplier; 
     controller.Move (moveVector * Time.deltaTime); 

     if (controller.isGrounded) { 
      VerticalSpeed = 0.0f; 
      jump = true; 
     } else { 
      VerticalSpeed = gravity; 
      jump = false; 
     } 

     if (Input.GetKey(KeyCode.X)) { 
      if(jump == true) { 
       VerticalSpeed += 25f; 
     } 
    } 
    } 
} 

回答

1

你可以嘗試改變垂直速度在else反映隨着時間的變化。

也許是這樣的:

VerticalSpeed += gravity * Time.deltaTime

而不是僅僅將其設置爲重心。你可能需要用你的初始跳躍速度來讓它感覺更好,但是這應該開始你的跳躍速度,當你跳到最高點時減速,當你下降時加速。

+0

謝謝!有效。 – SoulPixel