简体   繁体   English

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

[英]C# Unity Character jumps really weird

Hello guys! 大家好!

I have a problem with my UNITY 5 code. 我的UNITY 5代码有问题。

My character can jump but he jumps instantly and too fast. 我的角色可以跳跃,但他会立即跳得太快。 It looks really weird. 看起来很奇怪。

I would greatly appreciate your feedback on my code! 非常感谢您对我的代码的反馈!

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;
        }
    }
    }
}

You can try changing the vertical speed in your else to reflect a change over time. 您可以尝试更改else的垂直速度,以反映随时间的变化。

Perhaps something like: 也许是这样的:

VerticalSpeed += gravity * Time.deltaTime

Instead of just setting it to gravity. 而不是仅仅将其设置为引力。 You may need to play with your initial jump speed to get it to feel better, but this should start your jump fast-ish, slow down as you reach the top of the jump, and speed back up as you fall. 你可能需要使用你的初始跳跃速度来让它感觉更好,但是这应该开始你的快速跳跃,当你到达跳跃的顶部时减速,并在你跌倒时加速回升。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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