- Home /
Change animation of 4 Way Movement Sprites moving diagonally to last used non-diagonal direction?
I have currently setup a number of animations with sprites that covers 4 way movement (up/down, left/right) and have set these up within the Animator with Blend Trees to switch between them depending on the player input.
This current setup allows for directional movement but only uses the up/down sprites when doing so - for instance, when the blend tree is set at -0.5 for both horizontal and vertical (exactly mid-way between left and down) the animation chosen is the down animation. (See Gif below, sprite only uses up/down for diagonal movement)
How can I get this to change depending on the last direction used, so for example, it'd act in the following ways instead:
Player is going left/right, then also presses the up/down key to go diagonally up/down to the left/right. (Animator keeps spite animating with left/right animation)
Player is going up/down, then also presses the left/right key to go diagonally up/down to the left/right. (Animator keeps spite animating with up/down animation)
I assume, like how I currently record the last vertical/horizontal movement for the idle blend trees - I would need to do something similar to capture the last (non-diagonal) movement, with some if statements on the movement function for example:
// Set Animator Parameter 'LastNonDiagonal'
// 0 = Up, 1 = Down, 2 = Left, 3 = Right, 4 = Error/Not Yet Moved
if ((move.x == 0) && (move.y > 0))
{
charAnim.SetFloat("LastNonDiagonal", 0);
}
else if ((move.x == 0) && (move.y < 0))
{
charAnim.SetFloat("LastNonDiagonal", 1);
}
else if ((move.x < 0) && (move.y == 0))
{
charAnim.SetFloat("LastNonDiagonal", 2);
}
else if ((move.x > 0) && (move.y == 0))
{
charAnim.SetFloat("LastNonDiagonal", 3);
}
else
{
charAnim.SetFloat("LastNonDiagonal", 4);
}
But I'm not certain on how exactly I could then use such a value to override the default behaviour of favouring the up/down animations within the blend tree.