Question by
Caphter · May 22, 2021 at 09:57 AM ·
2d gameshootingenemy aitransform.rotation
Let object face player with fixed origin 2D
Hello, I want to instantiate an object that is rotated towards the players current position and has its fixed origin in the firepoint of the enemy (all in 2D space).
However I couldnt find any soultion in any thread, that works for me.
I tried multiple variants of the transform.lookAt(targetPosition)
, but apparently it only works for 3D and as soon as I try to use it my object gets deletet after the first frame of the instantiation. I also tried stuff like:
private void LookAt2D(Transform transform, Vector2 target)
{
Vector2 current = transform.position;
var direction = target - current;
var angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
With this solution the aim on the player works, but the object doesnt spawn at the firepoint, but somewhere in the room.
Thanks for an answer!
Comment