Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Moving the Player Diagonally

Discussion in 'Animation' started by omern92, Dec 28, 2023.

  1. omern92

    omern92

    Joined:
    May 31, 2022
    Posts:
    4
    Hi all,
    I have the following snippet (see below) for moving the Player around according to inputs via Unity's new Input System.
    I have the following problem/question:
    I want the player to move smoothly from forward to left/right.
    I have animations for Running, and Running fast, but they are forward only.

    So I tried to manipulate it via code with:
    Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * rotationSpeed);

    Although it does the work, I don't know if it's the best practice and a good method, because I read that Lerp
    is overused unnecessarily and that it consumes a lot of resources.
    What are your recommendations?

    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         // Read the input vector from the "Movement" action
    4.         Vector2 inputVector = inputActionsPlayer.Player.Movement.ReadValue<Vector2>();
    5.  
    6.         // Convert the input vector to a direction vector in world space
    7.         Vector3 direction = new Vector3(inputVector.x, 0, inputVector.y);
    8.  
    9.         // Move the player in the input direction if there is input
    10.         if (direction.magnitude > 0.01f)
    11.         {
    12.             // Update the last direction to the input direction
    13.             lastDirection = direction.normalized;
    14.  
    15.             // Update the last non-zero direction to the input direction
    16.             if (lastDirection.magnitude > 0.01f)
    17.             {
    18.                 lastNonZeroDirection = lastDirection;
    19.             }
    20.  
    21.             //// Animation Sector ////
    22.             anim.SetBool("IsMoving", true);
    23.             moveTime += Time.deltaTime;
    24.             anim.SetFloat("MoveTime", moveTime);
    25.  
    26.             while (moveTime > 1.9f)
    27.             {
    28.                 moveTime = 1.9f;
    29.             }
    30.             // Move the player in the input direction
    31.             transform.position += lastDirection * speed * moveTime * Time.deltaTime;
    32.         }
    33.         else
    34.         {
    35.             //// Animation Sector ////
    36.             anim.SetBool("IsMoving", false);
    37.             moveTime = 1f;
    38.             anim.SetFloat("MoveTime", moveTime);
    39.         }
    40.  
    41.         // Rotate the player towards the last direction or last non-zero direction if there is no input
    42.         if (lastDirection.magnitude > 0.01f)
    43.         {
    44.             // Calculate the target rotation based on the last direction
    45.             float angle = Mathf.Atan2(lastDirection.x, lastDirection.z) * Mathf.Rad2Deg;
    46.             Quaternion targetRotation = Quaternion.Euler(0, angle, 0);
    47.  
    48.             // Interpolate between the current rotation and the target rotation
    49.             transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * rotationSpeed);
    50.         }
    51.         else if (lastNonZeroDirection.magnitude > 0.01f)
    52.         {
    53.             // Calculate the target rotation based on the last non-zero direction
    54.             float angle = Mathf.Atan2(lastNonZeroDirection.x, lastNonZeroDirection.z) * Mathf.Rad2Deg;
    55.             Quaternion targetRotation = Quaternion.Euler(0, angle, 0);
    56.  
    57.             // Interpolate between the current rotation and the target rotation
    58.             transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.deltaTime * rotationSpeed);
    59.         }
    60.  
    61.     }