- Home /
Why does the input not go back to zero when controls are released?
using System.Collections;
using UnityEngine;
public class Player : MonoBehaviour {
public Rigidbody rb;
public float movementMultiplier = 0.05f;
private void FixedUpdate()
{
float leftJoyHorizontal = Input.GetAxis("LeftJoystickHorizontal");
float leftJoyVertical = Input.GetAxis("LeftJoystickVertical");
float rightJoyHorizontal = Input.GetAxis("RightJoystickHorizontal");
float rightJoyVertical = Input.GetAxis("RightJoystickVertical");
float leftTrigger = Input.GetAxis("LeftTrigger");
Vector3 rotate = new Vector3(-rightJoyVertical, -rightJoyHorizontal, 0f);
Debug.Log(leftJoyHorizontal + " " + leftJoyVertical);
rb.AddRelativeForce(leftJoyHorizontal*movementMultiplier, -leftJoyVertical*movementMultiplier, 0f, ForceMode.Force);
}
}
If you see the Debug.log(), it logs the value of the left stick to the console. When the left is moved, the value is not 0, but when the stick is back in the center, the values do not update back to zero, as a result of this, my character keeps moving in the direction it was previously given.
Are your joysticks virtual or physical? If they are virtual, you will need to set their value to 0 and position to center yourself in the script.
This is most likely a problem with deadzones. $$anonymous$$ost joysticks (360 ones especially) do not fully reset the thumbsticks to the center. There is a small amount at which the sticks are free to move. Ordinarily, this is fixed by increasing the 'deadzone'; a value under which any joystick movement is not measured, working as a cutoff.
Answer by Bunny83 · Sep 10, 2017 at 10:33 AM
You apply a force to the rigidbody depending on your input. When you stop applying the force (i.e. the input turns back to 0) the rigidbody won't stop moving. It's like pushing a cart so it starts moving. When you stop pushing it still has velocity and will continue to move. It might come to halt due to friction after some time but never immediately.
You can simulate friction by increasing the "drag" value of the rigidbody. Of course a highter drag means you need more force to move it.
The reason why you might not see the input turns back to 0 might be that you have enabled "collapse" in your console. It will collapse the exact same log messages into one and just increase the count value.