I have a game with a beat-em-up perspective (think Final Fight, Streets of Rage) where movement is in X and Z. My movement logic consists of checking controller input of X-axis for X movement and Y-axis for Z movement. I use this logic to move enemy as well, using this method:
internal bool MoveTowardsPosition(Vector3 targetPosition)
{
bool reachedPosition = false;
bool reachedX = false;
bool reachedZ = false;
// Determine x direction
if (Mathf.Round(Mathf.Abs(targetPosition.x - _enemy.transform.position.x)) > 0)
{
if (targetPosition.x < _enemy.transform.position.x)
Input.HorizontalMove = -1;
else
Input.HorizontalMove = 1;
}
else
reachedX = true;
// Determine z direction
if (Mathf.Round(Mathf.Abs(targetPosition.z - _enemy.transform.position.z)) > 0)
{
if (targetPosition.z < _enemy.transform.position.z)
Input.DepthMove = -1;
else
Input.DepthMove = 1;
}
else
reachedZ = true;
if (reachedX && reachedZ)
reachedPosition = true;
return reachedPosition;
}
So what happens here is that given a target position, I add to the HorizontalMove (X) and DepthMove (Z) until the enemy reaches the destination (these X and Z additions will be read on Update to do a transform.translate()). The enemy does move to the destination but in a unnatural path. So when Z is larger, it will reach the destination X first then traverse Z. So for example, X=2 and Z=5, it'll traverse with a path like this:
What I'm trying to do is for it to traverse a path like this:
I figured instead of adding fixed 1/-1, I need to determine X and Z based on that angle but I'm lost on how to compute/do it. Can you guys lend me a hand, please?