Search Unity

Question Jaggy movement with gamepad

Discussion in 'Input System' started by mrCharli3, Oct 20, 2023.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    My movement works perfectly with WASD, but when using gamepad my character stutters and behaves strangely, same code.

    No idea what could be causing this?

    Code (CSharp):
    1.  public void ExecuteLogic()
    2.     {
    3.         var movement = InputManager.Input.Basic.Movement.ReadValue<Vector2>();
    4.      
    5.         player.movementDirection = new Vector3(movement.x, 0, movement.y).normalized;
    6.  
    7.         if (!player.overrideRotation)
    8.         {
    9.             // Rotate the character to face the movement direction
    10.             if (player.movementDirection.magnitude >= 1f)
    11.             {
    12.                 Quaternion targetRotation = Quaternion.LookRotation(player.movementDirection);
    13.                 player.transform.rotation = Quaternion.Lerp(player.transform.rotation, targetRotation, Time.deltaTime * player.config.rotationSpeed);
    14.             }
    15.         }
    16.  
    17.         // Movement
    18.         if (player.movementDirection.magnitude >= 1f)
    19.         {
    20.             player.characterController.Move(player.movementDirection * Time.deltaTime * player.config.movementSpeed);
    21.         }
    22.  
    23.         // Always ground character, some rare occasions collisions will cause it to levitate
    24.         player.transform.position = new Vector3(player.transform.position.x, player.groundY, player.transform.position.z);
    25.     }
     
  2. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976

    It was due to checking magnitude before moving...