Home > Net >  Unity jump script delay
Unity jump script delay

Time:09-17

Im making a 3d game in unity, and so I made a cs script for movement of my charecter, walking and moveing the camera works fine, however when i added the jump function, it had a delay. You could press the jump button 5 times, with no result. Then you press it again, and it jumps. I cant figure out why this does this.

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public CharacterController characterController;
    public int speed = 6;
    public float gravity = 9.87f;
    private float verticalspeed = 0;
    private Vector3 moveDirection = Vector3.zero;

    public Transform Camera;

    public float Sensitivity = 2f;
    public float uplimit = -50;
    public float downlimit = -50;
    public float jumpspeed = 5.0f;


    void Update()
    {
        move();
        cameramove();
        

        void cameramove()
        {
            float horizontal = Input.GetAxis("Mouse X");
            float vertical = Input.GetAxis("Mouse Y");

            transform.Rotate(0, horizontal * Sensitivity, 0);
            Camera.Rotate(-vertical * Sensitivity, 0, 0);

            Vector3 currentRotation = Camera.localEulerAngles;
            if (currentRotation.x > 180) currentRotation.x -= 360;
            currentRotation.x = Mathf.Clamp(currentRotation.x, uplimit, downlimit);
            Camera.localRotation = Quaternion.Euler(currentRotation);
        }

        void move()
        {
            float horizontalMove = Input.GetAxis("Horizontal");
            float verticalMove = Input.GetAxis("Vertical");

            if (characterController.isGrounded) verticalspeed = 0;
            else verticalspeed -= gravity * Time.deltaTime;

            Vector3 gravityMove = new Vector3(0, verticalspeed, 0);
            Vector3 move = transform.forward * verticalMove   transform.right * horizontalMove;
            characterController.Move(speed * Time.deltaTime * move   gravityMove * Time.deltaTime);
            if (characterController.isGrounded && Input.GetButton("Jump"))
            {
                moveDirection.y = jumpspeed;
            }
            moveDirection.y -= gravity * Time.deltaTime;
            characterController.Move(moveDirection * Time.deltaTime);



        }

    }
}

CodePudding user response:

Assuming your character has a RigidBody assigned to it, you can refer it on your script as,

public class PlayerMovement : MonoBehaviour
{
    private Rigidbody myrigidbody;

    void Start () {
        myrigidbody = GetComponent<Rigidbody>();
    }
}

Then you can pass the jumpspeed as a Vector3, and probably you might have to trigger your animation here as well.

if (characterController.isGrounded && Input.GetButton("Jump"))
{
     characterController.isGrounded = false;
     myrigidbody.AddForce(new Vector3(0, jumpspeed, 0));
     // Trigger your animation, myanimator.SetTrigger("jump");
}

CodePudding user response:

Unity character controller move documentation also contains jump function.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Example : MonoBehaviour
{
    private CharacterController controller;
    private Vector3 playerVelocity;
    private bool groundedPlayer;
    private float playerSpeed = 2.0f;
    private float jumpHeight = 1.0f;
    private float gravityValue = -9.81f;

    private void Start()
    {
        controller = gameObject.AddComponent<CharacterController>();
    }

    void Update()
    {
        groundedPlayer = controller.isGrounded;
        if (groundedPlayer && playerVelocity.y < 0)
        {
            playerVelocity.y = 0f;
        }

        Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        controller.Move(move * Time.deltaTime * playerSpeed);

        if (move != Vector3.zero)
        {
            gameObject.transform.forward = move;
        }

        // Changes the height position of the player..
        if (Input.GetButtonDown("Jump") && groundedPlayer)
        {
            playerVelocity.y  = Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
        }

        playerVelocity.y  = gravityValue * Time.deltaTime;
        controller.Move(playerVelocity * Time.deltaTime);
    }
}
  • Related