Home > Blockchain >  Slowly decrease movement speed after releasing left shift
Slowly decrease movement speed after releasing left shift

Time:02-23

So, I am still developing a game in Unity, where my only problem now is that I can't figure out how to slowly decrease the speed of my character after releasing left shift. Slowly increasing speed while holding shift is the only one I had worked out because I used the incrementing technique, but the speed slowly decreasing after releasing left shift is not. Can someone help me? Here's the code, I apologize, I am literally a beginner at this:

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

public class PlayerMovement : MonoBehaviour
{
    public CharacterController controller;

    //Codes for setting walk and sprint speed (You should put speed in "Default Speed". "Walk Speed" is public so I can see if my speed is increasing)
    public float defaultSpeed = 8f;
    public float walkSpeed = 8f;
    public float sprintIncrease = 15f;
    public float sprintLimit = 16f;

    //Gravity force
    public float gravity = -60f;

    //Probably jump height?
    public float jumpHeight = 2f;

    //For checking if you're on the ground
    public Transform groundCheck;
    public float groundDistance = 0.5f;
    public LayerMask groundMask;

    //Code to check if you are on the ground
    Vector3 velocity;
    bool isGrounded;

    // Update is called once per frame
    void Update()
    {

        //For checking again if you're on the ground
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        //Code to slowly increase sprint FORWARD while holding left shift
        if (Input.GetKey(KeyCode.LeftShift) && Input.GetAxis("Vertical") > 0)
        {
            walkSpeed  = sprintIncrease * Time.deltaTime;  
        
        }
        //Code to prevent sprinting while holding S and when combined with A or D
        else if (Input.GetKey(KeyCode.LeftShift) && Input.GetAxis("Vertical") < 0)
        {
            walkSpeed = defaultSpeed;
        }

        //Code for allowing to slowly increase sprint LEFT sideways
        else if (Input.GetKey(KeyCode.LeftShift) && Input.GetAxis("Horizontal") < 0)
        {
            walkSpeed  = sprintIncrease * Time.deltaTime;
        }

        //Code for allowing to slowly increase sprint RIGHT sideways
        else if (Input.GetKey(KeyCode.LeftShift) && Input.GetAxis("Horizontal") > 0)
        {
            walkSpeed  = sprintIncrease * Time.deltaTime;
        }

        //To stop Sprint Speed while holding shift from exceeding Sprint Limit
        if (walkSpeed > sprintLimit)
        {
            walkSpeed = sprintLimit;
        }

        //Executes when shift is released, to slowly go back to default speed(This is my problem) 
        if (Input.GetKeyUp(KeyCode.LeftShift))
        {
            walkSpeed = defaultSpeed;
        }

        //To check if you're grounded so velocity doesn't increase
        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }

        //The basic horizontal and vertical axises
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        //The basic code for movement
        Vector3 move = transform.right * x   transform.forward * z;

        controller.Move(move * walkSpeed * Time.deltaTime);

        //The code for jumping
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }

        velocity.y  = gravity * Time.deltaTime;

        controller.Move(velocity * Time.deltaTime);
    }
}

I thank everyone in advance.

CodePudding user response:

if (!Input.GetKey(KeyCode.LeftShift))
{
    walkSpeed -= sprintIncrease * Time.deltaTime;
}

the above code will do what you asked for. Just a heads up, the way you're implementing stuff is beginner-friendly but not performance friendly.

CodePudding user response:

Ok, I have figured this out myself. I rearrange my ifs and else statement to look more organized. I added the else statement where if you don't hold shift while walking, it continuously subtracts the walk speed with the sprint increase, but to stop it from being lower than the default speed, I added an if statement where if the walk speed becomes lower than the default speed, it becomes equal to the default speed. Note that I rearranged my statements to trigger an else statement.

using System.Collections;

using System.Collections.Generic; using UnityEngine;

public class PlayerMovement : MonoBehaviour { public CharacterController controller;

//Codes for setting walk and sprint speed (You should put speed in "Default Speed". "Walk Speed" is public so I can see if my speed is changing or not)
public float defaultSpeed = 8f;
public float walkSpeed = 8f;
public float sprintIncrease = 15f;
public float sprintLimit = 16f;

//Gravity force
public float gravity = -60f;

//Probably jump height?
public float jumpHeight = 2f;

//For checking if you're on the ground
public Transform groundCheck;
public float groundDistance = 0.5f;
public LayerMask groundMask;

//Code to check if you are on the ground
Vector3 velocity;
bool isGrounded;

// Update is called once per frame
void Update()
{

    //For checking again if you're on the ground
    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

    //Code to prevent sprinting while holding S and when combined with A or D(New Code)
    if (Input.GetKey(KeyCode.LeftShift) && Input.GetAxis("Vertical") < 0)
    {
        walkSpeed -= sprintIncrease * Time.deltaTime;
    }

    //Code to slowly increase sprint FORWARD while holding left shift
    else if (Input.GetKey(KeyCode.LeftShift) && Input.GetAxis("Vertical") > 0)
    {
        walkSpeed  = sprintIncrease * Time.deltaTime;                     
    }

    //Code for allowing to slowly increase sprint LEFT sideways
    else if (Input.GetKey(KeyCode.LeftShift) && Input.GetAxis("Horizontal") < 0)
    {
        walkSpeed  = sprintIncrease * Time.deltaTime;
    }

    //Code for allowing to slowly increase sprint RIGHT sideways
    else if (Input.GetKey(KeyCode.LeftShift) && Input.GetAxis("Horizontal") > 0)
    {
        walkSpeed  = sprintIncrease * Time.deltaTime;
    }

    //To make sure that your speed decrease smoothly if not holding left shift(New code)
    else
    {
        walkSpeed -= sprintIncrease * Time.deltaTime;
    }

    //To stop Sprint Speed while holding shift from exceeding Sprint Limit
    if (walkSpeed > sprintLimit)
    {
        walkSpeed = sprintLimit;
    }

    //To stop Walk Speed from getting lower than Default Speed(New Code)
    if (walkSpeed < defaultSpeed)
    {
        walkSpeed = defaultSpeed;
    }

    //To check if you're grounded so velocity doesn't increase
    if (isGrounded && velocity.y < 0)
    {
        velocity.y = -2f;
    }
    
    //The basic horizontal and vertical axises
    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

    //The basic code for movement
    Vector3 move = transform.right * x   transform.forward * z;

    controller.Move(move * walkSpeed * Time.deltaTime);

    //The code for jumping
    if (Input.GetButtonDown("Jump") && isGrounded)
    {
        velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    }

    velocity.y  = gravity * Time.deltaTime;

    controller.Move(velocity * Time.deltaTime);
}
}
  • Related