Player Sliding without any Input

I have a player movement script that tracks if a user is mounted on an object, and if isn’t, then the player can move. After all input has stopped, the player continues to slide. I’ve done quite a bit of research and can’t seem to find anything that works:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using UMA.CharacterSystem;

public class UnmountedController : MonoBehaviour
{
    public Animator anim;
    public bool isRunning = false;
    LoginUser Login;
    MountHorse MountHorseScript;
    public bool isMounted = false;
    public bool isMoving;

    void Update()
    {

            MountHorseScript = GameObject.Find("ScriptManager").GetComponent<MountHorse>();
            isMounted = MountHorseScript.Mounted;

        if(!isMounted){
            float vertical = Input.GetAxis("Vertical");
            float horizontal = Input.GetAxis("Horizontal");
            
            
                transform.Rotate(0, Input.GetAxis("Horizontal") * 2, 0);
            if(Input.GetKeyDown(KeyCode.RightShift) || Input.GetKeyDown(KeyCode.LeftShift)){
                if(isRunning){
                    isRunning = false;
                }else{
                    isRunning = true;
                }
            }

            bool hasHorizontalInput = !Mathf.Approximately(horizontal, 0f);
            bool hasVerticalInput = !Mathf.Approximately(vertical, 0f);
            isMoving = hasVerticalInput;
            if(isMoving == true){
                if(isRunning){
                    transform.position += transform.forward * 6 * vertical * Time.deltaTime;
                }else{
                    transform.position += transform.forward * 2 * vertical * Time.deltaTime;
                }
            }
            
            anim.SetBool("Moving", isMoving);
            anim.SetBool("Running", isRunning);
        }else{}
        
            anim.SetBool("Mounted", isMounted);
        
    }
}

Thanks in advance!

Hi @girlinorbit, it looks like the position should stop changing once your vertical input is 0. Have you verified that with all input stopped the vertical input is 0? You can use Debug.Log statements to see all the values involved with the logic in your code when you see unintended behavior like this. Just add the following line in your code right before the isMoving == true check (which can simplify just to if (isMoving) and check the console when you are “continuing to slide”:

Debug.Log("Vertical: " + vertical + " hasVertical: " + hasVertical + " isMoving: " + isMoving);

Then you can see if some of your bools or the vertical float value is not what you expect.

Also:

             if(isRunning){
                 isRunning = false;
             }else{
                 isRunning = true;
             }

simplifies to:

isRunning = !isRunning;

Let us know what you find out!

When I log the input, after the player stops walking, I get this:

Vertical: 0 hasVertical: False isMoving: False

and the player still slides.

I have tried messing with the rigidbody constraints and that hasn’t helped either.