Y
Y
Yura Mikhailov2020-10-23 18:15:36
Unity
Yura Mikhailov, 2020-10-23 18:15:36

Why does sprint keep running after triggering a jump?

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

public class CharacterControl: MonoBehaviour
{
    //скорость персонажа
    public float speed = 4.0f;
    //скорость прыжка персонажа
    public float jumpSpeed = 8.0f;
    //переменная гравитации персонажа
    public float gravity = 20.0f;
    // переменная движения персонажа 
    private Vector3 moveDir = Vector3.zero;
    //переменная CharacterController
    private CharacterController controller;
    
    void Start()
    {
        controller = GetComponent<CharacterController>();

    }

    void Update()
    {
        if (controller.isGrounded)
        {
            
            moveDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            moveDir = transform.TransformDirection(moveDir);
            moveDir *= speed;
        }

        if(Input.GetKeyDown(KeyCode.Space)&& controller.isGrounded)
        {
            moveDir.y = jumpSpeed;
        }

        moveDir.y -= gravity * Time.deltaTime;
        controller.Move(moveDir * Time.deltaTime);

         //начал делать спринт и вот здесь косяк
         if (Input.GetKey(KeyCode.LeftShift)&& controller.isGrounded)
        {
            speed = 10.0F; 
        }
         else if (Input.GetKeyUp(KeyCode.LeftShift) && controller.isGrounded)
       {
            speed = 4.0F;
        }
    }
}


If you jump while sprinting, the sprint seems to get stuck and waits for another press of the shift to return to normal speed. Who can faced similar?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question