New Player State - animation not aligning with movement

I have completed the Unity 3rd Person Combat & Traversal course which was very instructional on creating a player controller via a state machine. Loved the format and detailed explanations to help us understand.

I’ve been tweaking the controller to customize it to how I wanted my game to work which is similar to Elden Ring. I have been attempting to modify the PlayerDodgingState to use a rolling animation instead of the dash.

I am able to run the rolling animation but the player is only rolling in place and not being moved. Given the Tick logic of the state I feel it has everything I need to move the player in the direction of the dodge using the new Rolling animation:

    public override void Tick(float deltaTime)
    {
        Vector3 movement = new Vector3();

        movement += _stateMachine.transform.right * _dodgingDirectionInput.x * _stateMachine.DodgeLength / _stateMachine.DodgeDuration;
        movement += _stateMachine.transform.forward * _dodgingDirectionInput.y * _stateMachine.DodgeLength / _stateMachine.DodgeDuration;

        Move(movement, deltaTime);
        //FaceTarget();

        _remainingDodgeTime -= deltaTime;

        if (_remainingDodgeTime <= 0f)
        {
            ReturnToLocomtion();
        }
    }

I have tried changing the DodgeLength and DodgeDurations but the new Rolling animation closely matches the same specs as the Dash animation used in the course. However, when the character rolls he only does so in place and not moving on the screen.

The only true changes I made to support this was to disable the character controller in the Start method and re-enabled in the Exit method as well as commenting out the FaceTarget as I wanted the player facing the direction they are rolling and then when the state switches the player’s direction can be updated given their new state.

Any ideas how to force the player to move in the Dodge direction while the animation is running?

Thanks,
Craig

Move uses the character controller, so disabling it is disabling the movement. I’m not sure why you disabled it, but you shouldn’t have.

Good Point! I removed the disablement of the controller but the issue continues with the odd behavior. I have reset the code back to its original content as defined in the course.

At this point I am only trying to get the character to be able to roll successfully as it transitions from the PlayerFreeLookState (I’m allowing rolling from that state as well as Targeting).

The character is able to successfully roll forward. If I try to run towards the camera and roll then the character animation runs correctly but the player actually moves in the opposite direction away from the camera. If I am running left/right and transition to roll the animation again runs perfectly in the correct direction but the movement actually pulls the character somewhat towards the camera at an odd angle.

I am new to the Cinemachine and realize now that my transition from PlayerTargetingState is somewhat being impacted by the camera which is why I am just focusing on the transition from the FreeLookState first which is a bit more straightforward. Any ideas what might be causing the odd behavior?

It’s probably root motion. I wish I knew more about it but all my attempts to get it to work properly always ends up being a mess.

Sometimes the motion is part of the animation. I usually use mixamo animations and can choose to not have that included in the animation before I download it. But if you don’t have that option, you may need to open the animations and clean it up yourself.

Actually I am using Kevin Iglesias’s animations in the asset store. He has two versions for rolling, normal and root motion. I am only using the non-root motion given the same experience you had. I’m thinking the odd behavior is some how related to the use of Cinemachine. I’ll keep playing with it but appreciate your thoughts.

It sounds like the movement isn’t being properly calculated for the current camera… The tricky part about this is that you probably don’t want to write two distinct Dodge states (I certainly wouldn’t), one from FreeLookState and one from TargetingState.

You might consider adding a Vector3 parameter to the constructor of PlayerDodgeState(). Then in your OnDodge() method, call it with

stateMachine.SwitchState(new PlayerDodgeState(stateMachine, CalculateMovement()));

This will automatically calculate the movement based on the state we’re in. In PlayerFreeLookState, it will orient the dodge to the camera’s forward. In PlayerTargetingState, it will orient the dodge to the Player’s forward orientation.

That did help remove the pulling of the player towards the camera like I was seeing earlier. But the rolling animation is still occurring in place. The minute the state machine moves to the DodgeState the player starts the rolling animation and stops moving altogether until the animation completes and the state is returned to FreeLook.

    public override void Tick(float deltaTime)
    {
        Vector3 movement = new Vector3();

        movement = (_stateMachine.transform.right * _dodgingDirectionInput.x) * (_stateMachine.DodgeLength / _stateMachine.DodgeDuration);
        movement += (_stateMachine.transform.forward * _dodgingDirectionInput.y) * (_stateMachine.DodgeLength / _stateMachine.DodgeDuration);

        Move(movement, deltaTime);
        //FaceTarget();

        _remainingDodgeTime -= deltaTime;

        if (_remainingDodgeTime <= 0f)
        {
            ReturnToLocomotion();
        }
    }

Maybe I am misunderstanding the point of the movement assignment in this method. I thought that multiplying the right and forward transforms by the offset of the duration (DodgeLength / DodgeDuration) was to increment the movement of the player to continue moving giving the roll a more natural look.

Did I misunderstand that part?

Also, just to rule out the possibility of the animation being the cause in some way, I switched the roll out with the normal walk movement and got the same behavior. The player will walk in place during the dodge state until the dodge completes and then will start walking normal within the FreeLook state. So I don’t believe the roll itself is the cause.

Thanks,
Craig

Once you have passed the CalculateMovement() from the FreeLook or TargetingState, this new movement value is what you will be using:

Vector3 dodgeMovement;
public PlayerDodgeState(PlayerStateMachine statemachine, Vector3 dodgeDirection) : base (statemachine)
{
     dodgeMovement = dodgeDirection;
}

public void Tick(float deltaTime)
{
    Move(dodgeMovement * _stateMachine.DodgeSpeed, deltaTime);
}

This is of course, simplified (I’m not including the Enter, Exit, or the logic for dodge time, but the idea is that you’ve set your movement when you actually invoke the dodge. The remainder of the dodge is simply continuing in that direction. This is similar to how we pass the momentum from the PlayerJumpingState to the PlayerFallingState.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms