简体   繁体   中英

C# Unity Character jumps really weird

Hello guys!

I have a problem with my UNITY 5 code.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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