top down 2D : bad enemy shooting angle
hi guys! im trying to build my first project and i'd like some input for a math correction. its about a top down 2D shooter, a bit like hotline miami.
when my enemy sees the player, it moves in its direction and stops at fire range to shoot him. the annoying thing is that it aims very bad (i should have made stormtroopers). most shots miss the player (near misses) except in certain directions; im pretty sure its because the enemy isnt accurately enough rotated 'in front' of the player, as the bullets are fired forward simply.
to rotate the enemy im using this :
Vector3 currentPosition = transform.position;
Vector3 nextPosition = player.transform.position;
Vector3 directionOfTravel = nextPosition - currentPosition;
float dist = Vector2.Distance(nextPosition, currentPosition);
directionOfTravel.Normalize();
//rotate the ennemy sprite in the correct direction if (directionOfTravel != Vector3.zero)
{
float angle = (Mathf.Atan2(directionOfTravel.y, directionOfTravel.x) * Mathf.Rad2Deg) - 90;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
if (dist < 1f)
{
rb.velocity = Vector3.zero;
ShootPlayer();
}
else
//move by the directionOfTravel vector components
{ rb.MovePosition(transform.position + directionOfTravel * runSpeed * Time.deltaTime);
return;
}
[1]: /storage/temp/71625-miss.png
Your answer
Follow this Question
Related Questions
Sprite gets rotated when Joystick Y at 1 or -1? 0 Answers
Rotation inverts forward direction 0 Answers
Camera Follow Behind A Rolling Ball 1 Answer
following and rotation help 0 Answers
World Rotation Around The Player Lead me to a Strange Result! 0 Answers