- Home /
How to make character move like beat'em up game ?
Hello everyone,
I'm creating a beat'em up game, and i'm stuck at movement part. I already made the first level (just a background). I'd like to know how to allow character to move in depth, like previous beat'em up games ? I did simple movement/attack/animation script, but it's only left/right/jump. How can I make player move like described in this picture ?
Many thanks to anyone who can help me.
Answer by Ymrasu · Mar 16, 2019 at 09:17 PM
This movement is very similar to top-down 2D movement, but you want to find a different direction to move instead of straight up and down. Here is some sample code with some comments to help explain:
// angles relative to right being 0 degrees (pos goes up, neg goes down)
float upDownAngle = 55;
float stairsAngle = -45;
Vector3 upDownDirection;
Vector3 stairsDirection;
float speed = 10;
bool onStairs;
void Start()
{
// turn angles into direction vectors
upDownDirection = new Vector3(Mathf.Cos(upDownAngle * Mathf.Deg2Rad), Mathf.Sin(upDownAngle * Mathf.Deg2Rad));
stairsDirection = new Vector3(Mathf.Cos(stairsAngle * Mathf.Deg2Rad), Mathf.Sin(stairsAngle * Mathf.Deg2Rad));
}
void Update()
{
// correct for diagonal movement with normalized
var moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")).normalized;
if (moveInput.y != 0) {
// apply with the new up/down direction vector instead of Vector3.up
transform.position += moveInput.y * upDownDirection * speed * Time.deltaTime;
}
if (moveInput.x != 0) {
// find some way to detect if on stairs
if (onStairs == true) {
transform.position += moveInput.x * stairsDirection * speed * Time.deltaTime;
} else {
// else move normally
transform.position += moveInput.x * Vector3.right * speed * Time.deltaTime;
}
}
}
You will need to find some way to detect when you are on the stairs. This is assuming you only want to go down stairs.
$$anonymous$$any thanks @ymrasu for that exhaustive answer. Does that sample can be used "as is" ? $$anonymous$$y first 2D character controller was from basic assets pack on Unity store + an animator script I wrote for punch/kick/walk/idle animations.
Yes, I have tested the code and you can put that directly on an object to begin moving it around with WASD or the arrow keys.
Do you have any more information on the problem you are getting? This C# code should also be inside the class if you just copied and pasted into a new file.