Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How can I move the player with roll animation?

Discussion in 'Animation' started by yekoba, Feb 21, 2023.

  1. yekoba


    Apr 26, 2022
    I have a 2D roll asset which is used to dodge and when I use that, the player's position doesn't change and after the animation, it returned its initial position. I tried to translate its position after the animation ends but it causes a jittering, how can I solve this?

    public void StopRolling()

    rb.MovePosition(new Vector2(rb.position.x + 0.54f, rb.position.y));
    animator.SetBool("isRolling", false);
    canMove = true;
    This is the code I used to stop the rolling and update its position. I added the animation as a gif.

    Attached Files:

  2. Unrighteouss


    Apr 24, 2018
    Generally, making assets like this is very bad practice. You want the character to animate in place so you can move it however you like. Leaving it like this can lead to all sorts of issues; for example, if you roll into a wall, your character will clip through the wall instead of rolling in place.

    I know it's a pain, but the best thing to do would be to edit the sprite sheet so that the character stays in place while animating; you can then give it a Rigidbody2D and CapsuleCollider2D and add whatever motion you desire through code.
  3. donotprikel


    Nov 14, 2022
    To move a player with a roll animation, you can follow these steps:

    1. Create a roll animation: First, create an animation of the player rolling. This animation should show the player moving forward while rolling. You can use software like Blender or Maya or even the built in unity animation creator to create this animation.

    2. Set up the player controller: You will need to create a script that controls the movement of the player. This script should allow the player to move forward and backward, left and right, and perform a roll animation.

    3. Trigger the roll animation: To trigger the roll animation, you can set up a key or button that the player can press. When the player presses this key or button, the roll animation should play.

    4. Apply movement during the animation: While the player is performing the roll animation, you will need to apply movement to the player. You can do this by setting the player's position or using a physics engine to apply forces.

    5. End the roll animation: Once the roll animation is complete, the player should return to their normal movement state.
    Heres a little example script ive got for you :)

    Code (CSharp):
    1. using UnityEngine;
    3. public class PlayerController : MonoBehaviour
    4. {
    5.     public float speed = 10.0f;
    6.     public float rollSpeed = 20.0f;
    7.     public float rollDuration = 1.0f;
    8.     public KeyCode rollKey = KeyCode.Space;
    10.     private bool isRolling = false;
    11.     private float rollTime = 0.0f;
    12.     private Vector3 rollDirection;
    14.     void Update()
    15.     {
    16.         float horizontal = Input.GetAxis("Horizontal");
    17.         float vertical = Input.GetAxis("Vertical");
    19.         if (!isRolling && Input.GetKeyDown(rollKey))
    20.         {
    21.             isRolling = true;
    22.             rollTime = 0.0f;
    23.             rollDirection = transform.forward;
    24.         }
    26.         if (isRolling)
    27.         {
    28.             rollTime += Time.deltaTime;
    29.             float rollProgress = rollTime / rollDuration;
    30.             float rollAngle = rollProgress * 360.0f;
    31.             transform.Rotate(Vector3.up, rollAngle);
    32.             transform.position += rollDirection * rollSpeed * Time.deltaTime;
    34.             if (rollTime >= rollDuration)
    35.             {
    36.                 isRolling = false;
    37.                 transform.rotation = Quaternion.identity;
    38.             }
    39.         }
    40.         else
    41.         {
    42.             Vector3 direction = new Vector3(horizontal, 0.0f, vertical);
    43.             direction.Normalize();
    44.             transform.position += direction * speed * Time.deltaTime;
    45.         }
    46.     }
    47. }
    This script can controll the player to move forward, backward, left, and right using the arrow keys or WASD keys on the keyboard. The player can perform a roll animation by pressing the spacebar or you can edit the script to make any key the rool animation hot key. During the roll animation, the player moves forward and rotates around the y-axis. Once the roll animation is complete, the player returns to their normal movement state. Note that this script assumes that the player is facing forward initially. If the player is facing a different direction, you may need to modify the rollDirection calculation to ensure that the player rolls in the correct direction.