Hello again folks. So… I’m trying to merge my player’s dodging state into my main RPG Core Combat Creator code, but I’m running into some funny issues, and I have no idea why. So, here’s my plan for this:
Split the Dodging state into two different aspects:
-
The Dodging in ‘PlayerFreeLookState’. Essentially, this one will be a simple animation, which is responsible for dodging when the player is freely roaming in the game. This one is meant to be a little more relaxing, and just something to use when you’re not in Combat. It could be for puzzles and other things…
-
The Dodging in ‘PlayerTargetingState’. This one is for the intense fights, and will include a full blend tree with about double the speed of the regular one, for the times when you actually need to evade a big hit or equivalent
The reason I’m splitting it up into two parts, is because it’s easier for me to read down the line
Anyway, currently I’m still working on developing the Free Look Dodging State, and here’s my script so far:
using RPG.States.Player;
using UnityEngine;
public class PlayerDodgingState : PlayerBaseState
{
private readonly int DodgeRollHash = Animator.StringToHash("DodgeRoll");
private readonly int FreeLookDodgeSpeedHash = Animator.StringToHash("FreeLookDodgeSpeed");
private Vector3 dodgingDirectionInput;
private float remainingDodgeTime;
private float dodgeDuration;
public PlayerDodgingState(PlayerStateMachine stateMachine, Vector3 dodgingDirectionInput) : base(stateMachine)
{
this.dodgingDirectionInput = dodgingDirectionInput.normalized;
}
public override void Enter()
{
Debug.Log($"Player Dodging State entered. Direction input is: {dodgingDirectionInput}");
dodgeDuration = GetDodgeAnimationLength();
remainingDodgeTime = dodgeDuration;
stateMachine.Animator.CrossFadeInFixedTime(DodgeRollHash, stateMachine.CrossFadeDuration);
// stateMachine.Health.SetInvulnerable(false);
}
public override void Tick(float deltaTime)
{
if (remainingDodgeTime > 0)
{
Vector3 movement = CalculateMovement(deltaTime);
Move(movement * stateMachine.FreeLookDodgingSpeed, deltaTime);
stateMachine.Animator.SetFloat(FreeLookDodgeSpeedHash, movement.magnitude, AnimatorDampTime, deltaTime);
remainingDodgeTime -= deltaTime;
if (remainingDodgeTime <= 0f)
{
stateMachine.SwitchState(new PlayerFreeLookState(stateMachine));
}
}
}
public override void Exit()
{
// stateMachine.Health.SetInvulnerable(false);
}
/// <summary>
/// Accumulate the total length of the Dodge animation you're playing,
/// to keep the transitions natural
/// </summary>
/// <returns></returns>
private float GetDodgeAnimationLength()
{
AnimatorClipInfo[] clipInfos = stateMachine.Animator.GetCurrentAnimatorClipInfo(0);
foreach (var info in clipInfos)
{
if (info.clip.name.Contains("Dodge"))
{
return info.clip.length;
}
}
return 1f; // if you can't find a 'Dodge' animation, return a value of 1
}
private Vector3 CalculateMovement(float deltaTime)
{
Vector3 movement = new Vector3();
movement += stateMachine.transform.right * dodgingDirectionInput.x * stateMachine.DodgeLength / GetDodgeAnimationLength();
movement += stateMachine.transform.forward * dodgingDirectionInput.y * stateMachine.DodgeLength / GetDodgeAnimationLength();
Debug.Log($"Movement * deltaTime value is: {movement * deltaTime}");
return movement * deltaTime;
}
}
My problem here is, the animation plays in place and he doesn’t physically move ahead when the animation plays, and I’m really confused as to why
This may also may or may not be important, but the animation I’m using is Non-Root Motion by default nature (I couldn’t find something static to work with)
Does anyone have any idea why this is the case?
And something else before I forget, what are the negative impacts of mixing Unity’s new Input System with the old ‘Input.GetKeyDown(KeyCode.(some button here));’? I just find that with specific keys, the new input system simply isn’t responsive 100% of the time, and it’s been bothering me for a while now (stuff like Running, Sprint-Swimming and Diving that require the Shift Key, and Dodging that demands the Control Key… all these buttons do not have a 100% response rate for some reason (i.e: They have some serious delays). For the time being, I’m using an awkward set of buttons that simply can never make it out in the final product, just for testing purposes)