- Home /
How to stop movement?
The issue I'm having is after letting go of the movement keys the player continues to move/slide in the direction they were going the only solution I've found was to set my moveDirection variable which is a Vector3 to zero, but I feel like this isn't an ideal way to do it, however, a lot of the common answers I've found related to this topic such as adjusting drag, using physics material, applying forces in the opposite direction haven't worked.
I'm using a RigidBody and Capsule Collider it doesn't have a physics material, it's not kinematic and the drag is 0 which I think is just all default and that's pretty much it.
Here is my movement code.
void Update()
{
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
inputDirection = new Vector3(horizontal, 0, vertical).normalized;
if (inputDirection.magnitude > 0)
{
float targetAngle = Mathf.Atan2(inputDirection.x, inputDirection.z) * Mathf.Rad2Deg + cam.eulerAngles.y; //basically moves in the correct direction the "+ cam.eulerAngles.y" is so it will move in the cameras direction as well
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime); //makes the player turn smoothly
transform.rotation = Quaternion.Euler(0f, angle, 0f);
moveDirection = (Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward).normalized; //new new move direction takes into account the camera's rotation
}
// else
// {
// moveDirection = Vector3.zero;
// }
}
void FixedUpdate()
{
//Debug.Log(transform.position + moveDirection * walkSpeed * Time.fixedDeltaTime);
rigidBody.MovePosition(transform.position + moveDirection * walkSpeed * Time.fixedDeltaTime);
rigidBody.AddForce(-transform.up * gravity, ForceMode.Acceleration); //gravity
}
Answer by Dag-Tholander · May 06 at 06:56 AM
The issue is that you check if (inputDirection.magnitude > 0). When the player gives no input, moveDirection will stay the same since inputDirection.magnitude > 0 returns false. You can fix this by adding the else statement to the code.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Making a bubble level (not a game but work tool) 1 Answer
enemy doesnt go towards small object 0 Answers
Slowing Down When Sliding Across Walls 2 Answers
Moving a object up 1 Answer