- Home /
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!
Is the constant sliding direction local to the player? If so there's something causing an offset to happen. Are there any other scripts referencing the player?
Answer by GrayLightGames · Nov 06, 2019 at 07:17 AM
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!
Answer by girlinorbit · Nov 06, 2019 at 07:49 PM
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.
Ah didn't realize you have a RigidBody on the player... I don't have as much experience with those, but try disabling the RigidBody component in the inspector during the motion to see if it continues. Is your RigidBody.velocity non-zero? Try forcing that to zero when the input is zero and see if it stops.
Have you tried adding friction by adjusting the mass of the RigidBody? Someone with more experience could weigh in, but I'm sure you could also find a good tutorial on implementing friction with a RigidBody in the meantime.
This could happen if the physics material friction combine is set to $$anonymous$$imum with fiction at a low value or/and gravity has been set really high. Also $$anonymous$$ penetration for penalty may be set to high as well. The default settings of the rigidbody component shouldn't have any of these issues which is why I wondered if the sliding was local to the player or not.
I tried removing the RigidBody but lost gravity. I did have a Physic $$anonymous$$aterial connected to my collider. I have been using U$$anonymous$$A, and didn't realize that it was there. Altering the mass of the rigidbody did help some, but after discovering the material I reset the component. Thank you all for your help! You've been amazing!
Your answer
Follow this Question
Related Questions
How do i maintain the same speed in the air? 1 Answer
Rotate game object and then return to its original rotation 1 Answer
Slime script? 1 Answer
New to Unity & Scripting, How Do I Create a Movement System Thats About Building Up Speed? 0 Answers
How to I put a speed limit on my character's movement? 1 Answer