繁体   English   中英

C#Unity角色跳跃真的很奇怪

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM