I have an enemy that has an attack AOE (area of effect, it is a trigger polygon collider) in which any objects will get damaged when the enemy attacks. I want the AOE to rotate towards the target (the player) around the enemy, but I can’t figure out how to do so. (I don’t want the enemy itself to rotate because it would ruin the pixel art sprites)
Here’s what I’ve tried so far:
float _zRotation = 0f;
//in 1st or 4th quadrants
if (player.position.x >= trans.position.x)
{
_zRotation = Mathf.Asin(player.position.normalized.y - trans.position.normalized.y) * Mathf.Rad2Deg;
}
else
{
//in 2nd quadrant
if (player.position.y >= trans.position.y)
{
_zRotation = 180f - (Mathf.Asin(player.position.normalized.y - trans.position.normalized.y) * Mathf.Rad2Deg);
}
//in 3rd quadrant
else
{
_zRotation = (Mathf.Asin(player.position.normalized.y - trans.position.normalized.y) * Mathf.Rad2Deg) + 180f;
}
}
attackAOE.transform.localEulerAngles = new Vector3(0f, 0f, 0f);
attackAOE.transform.Rotate(Vector3.forward, _zRotation);
I’ve taken this from my code that rotates a bullet in the direction of the mouse when it gets shot, which is this portion, made with my (rather limited) knowledge of trig:
Vector2 _mouseDir = Camera.main.ScreenToWorldPoint(_mouseScreenPos) - trans.position;
_mouseDir.Normalize();
float _zRotation;
//in 1st or 4th quadrants
if (_mouseScreenPos.x >= Screen.width / 2)
{
_zRotation = Mathf.Asin(_mouseDir.y) * Mathf.Rad2Deg;
}
else
{
//in 2nd quadrant
if (_mouseScreenPos.y >= Screen.height / 2)
{
_zRotation = 90 - (Mathf.Asin(_mouseDir.y) * Mathf.Rad2Deg) + 90f;
}
//in 3rd quadrant
else
{
_zRotation = (Mathf.Asin(-_mouseDir.y) * Mathf.Rad2Deg) + 180f;
}
}