How do I remove the glide in 3D unity movement?

Viewed 136

The Unity 3D movement has this built in feature where it glides when you stop while holding the key. How do I remove this glide?

Fair warning, I haven't tried anything yet. I am very new to Unity and assume its most likely just some button I need to click so please help.

Here's the code that I'm using:

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

public class movement : MonoBehaviour
{
    float moveSpeed;
    float walkSpeed = 3;
    float sprintSpeed = 6;

    public CharacterController controller;
    public float gravity = -9.81f;

    public Transform groundCheck;
    public float groundDistance = 8.4f;
    public LayerMask groundMask;
    Vector3 velocity;
    bool isGrounded;

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.LeftShift))
        {
            moveSpeed = sprintSpeed;
        }
        else
        {
            moveSpeed = walkSpeed;
        }
        
        
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }

        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;

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

        velocity.y += gravity * Time.deltaTime;

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

2 Answers

I'm more than happy to answer your question but I can't leave out this tidbit. I can't stress this enough. If you are new to Unity, you should avoid posting questions like this as much as possible. I'm not discouraging you from asking questions, but since you're new, really give it a go before you post anything. You'll learn soooo much more by removing variables and lines of code to figure out a solution on your own instead of getting the answer right away.

Regardless I believe the answer is in the last two lines of your code:

    velocity.y += gravity * Time.deltaTime;
    controller.Move(velocity * Time.deltaTime);

Let's think about this line by line. Dissect each line and try to really understand what they mean. As you run through your code, you are receiving three main inputs from the user:

    if (Input.GetKey(KeyCode.LeftShift))
    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

if (Input.GetKey(KeyCode.LeftShift)) is just a check to see if your user wants to run or not. The other two are receiving what is likely mouse movement input. These are primarily used to define the users movement. We know this because they are directly tied to these next two lines:

    Vector3 move = transform.right * x + transform.forward * z;
    controller.Move(move * moveSpeed * Time.deltaTime);

Now when you look closer at this, it tells us that if the user isn't moving the mouse, then the Vector3 move will be 0. That means that our player will stop moving and there should be no reason they glide. However, take a look at the next two.

    velocity.y += gravity * Time.deltaTime;
    controller.Move(velocity * Time.deltaTime);

These are not necessarily driven by user input. This means that considering they are tied to your controller and the other move calls aren't running without user input, these two lines must be the culprits for your glide problem.

So remove these and you should be good. However, I must make it clear, asking questions is good, but breaking the code to learn more about it is soo much better. As a challenge I think you should look into what drives velocity.y... or maybe isGrounded... and that will tell you more about why the glide is happening!

Hope this helps!

Check the "Gravity" field on your "Horizontal" and "Vertical" axes in the input manager (Edit > Project Settings, then select the Input category). If not 0, it will smooth out keyboard inputs.

Related