There is a drift in the movement, whenever I move (up, down, left, right). For example, when I move forward and then I stop pressing the key, the player continues to move in the same direction I was moving even though I stopped pressing! [I'm using the new input system].
[SerializeField] Movement movement;
[SerializeField] PlayerInteractions playerInteractions;
[SerializeField] MouseLook mouseLook;
PlayerControls.GroundMovementActions groundMovement;
Vector2 horizontalInput;
Vector2 mouseInput;
private void Awake()
{
controls = new PlayerControls();
groundMovement = controls.GroundMovement;
// groundMovement.[action].performed = context => do something
groundMovement.HorizontalMovement.performed = ctx => horizontalInput = ctx.ReadValue<Vector2>();
groundMovement.MouseX.performed = ctx => mouseInput.x = ctx.ReadValue<float>();
groundMovement.MouseY.performed = ctx => mouseInput.y = ctx.ReadValue<float>();
}
private void Update()
{
movement.ReceiveInput(horizontalInput);
mouseLook.ReceiveInput(mouseInput);
}
Movement
class:
private void stateHandler()
{
isGrounded = Physics.CheckSphere(transform.position, 0.1f, groundMask);
if (isGrounded)
{
verticalVelocity.y = 0;
}
// Jump: v = sqrt(-2 * jumpheight* gravity)
if (jump)
{
state = MovementState.jump;
if (isGrounded)
{
verticalVelocity.y = Mathf.Sqrt(-2f * jumpHeight * gravity);
}
jump = false;
}
verticalVelocity.y = gravity * Time.deltaTime;
controller.Move(verticalVelocity * Time.deltaTime);
if (running)
{
state=MovementState.running;
moveSpeed = runSpeed;
horizontalVelocity = (transform.right * horizontalInput.x transform.forward * horizontalInput.y) * moveSpeed;
controller.Move(horizontalVelocity * Time.deltaTime);
}
else
{
moveSpeed = walkSpeed;
horizontalVelocity = (transform.right * horizontalInput.x transform.forward * horizontalInput.y) * moveSpeed;
controller.Move(horizontalVelocity * Time.deltaTime);
state = MovementState.walking;
}
}
private void Update()
{
stateHandler();
}
public void ReceiveInput(Vector2 _horizontalInput)
{
horizontalInput = _horizontalInput;
}
MouseLook
class:
private void Update()
{
transform.Rotate(Vector3.up, mouseX * Time.fixedDeltaTime);
xRotataion -= mouseY;
xRotataion = Mathf.Clamp(xRotataion, -xClamp, xClamp);
Vector3 targetRotation = transform.eulerAngles;
targetRotation.x = xRotataion;
playerCamera.eulerAngles = targetRotation;
}
public void ReceiveInput(Vector2 mouseInput)
{
mouseX = mouseInput.x * sensitivityX;
mouseY = mouseInput.y * sensitivityY;
}
Here is what I tried to solve the issue:
groundMovement.MouseX.started = ctx => mouseInput.x = ctx.ReadValue<float>();
groundMovement.MouseX.canceled = ctx => mouseInput.x = ctx.ReadValue<float>();
groundMovement.MouseY.started = ctx => mouseInput.y = ctx.ReadValue<float>();
groundMovement.MouseY.canceled = ctx => mouseInput.y = ctx.ReadValue<float>();
but it did not work, it keeps drifting every time. How can I fix this?
CodePudding user response:
You want the character to stop moving but you have changed MouseX
and MouseY
. Add the following code.
groundMovement.HorizontalMovement.canceled = ctx => horizontalInput = Vector2.zero;