![](/img/trans.png)
[英]How do I use Input.GetButton() without getting several jumps at once? Unity 2D Character Controller C#
[英]C# Unity Character jumps really weird
大家好!
我的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;
}
}
}
}
您可以嘗試更改else
的垂直速度,以反映隨時間的變化。
也許是這樣的:
VerticalSpeed += gravity * Time.deltaTime
而不是僅僅將其設置為引力。 你可能需要使用你的初始跳躍速度來讓它感覺更好,但是這應該開始你的快速跳躍,當你到達跳躍的頂部時減速,並在你跌倒時加速回升。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.