#Top Down Character Controller with 8 Directions

1 messages · Page 1 of 1 (latest)

mortal kestrel
#

I am making a top down game with 8 animation directions. The movement is reading Input.GetAxis which is smoothed, so there's some nuanced in deciding how and when to change animation directions.

I have tried my hand a few times at creating my own system for this, but it's proving difficult to come up with an efficient, concise system, i.e. it often devolves into spaghetti.

An inspiration for what I'm going for would be CrossCode https://www.youtube.com/watch?v=QTwv5zKFYh4&list=PLGKJJhcJXlNwiTRYc2RhaVkOJinRk613q

Any tips for how to go about this?

CrossCode Walkthrough Part 1 No Commentary Gameplay

This retro-inspired 2D Action RPG might outright surprise you. CrossCode combines 16-bit SNES-style graphics with butter-smooth physics, a fast-paced combat system, and engaging puzzle mechanics, served with a gripping sci-fi story.

CrossCode is all about how it plays! That's why there is a f...

▶ Play video
fluid rose
#

You can get the angle of the input vector vs some reference (e.g. Vector2.Up) using Vector2.Angle(). Then this can be rounded to the closest 45 degree:
angle = Mathf.Round(angle / 45f) * 45f;

Then you can rotate the reference dir by the angle and get a new direction (use Quaternion.Euler() and then multiply against the ref angle).

full dock
#

Something like this would work for you?

string animName = value switch
{
    _ when value >= upThreshold => "Up",
    _ when value > 0 => "UpWards",
    _ when value <= -upThreshold => "Down",
    _ when value < 0 => "DownWards",
    _ => "Middle"
};

//flip the sprite if needed
bool facingLeft => movement.x < 0;```
fluid rose
#

Or when we round the angle to the closest 45 deg angle we can use this as a key (cast to int) for finding an animation