Hello everyone.
Im trying to make my sprite “face” another sprite (rotate on the Z axis to face it ingame).
And i tried using LookAt, but then it rotates on X & Y axis.
I need help with figuring out how to rotate on the Z axis only and face the other sprite. (The game is “top-down”)
Thanks!
I’m having to guess a bit. I’m assuming the camera is at negative ‘z’ looking towards positive ‘z’. I’m guessing that the up of the object is the side you want to face towards your target. If so, then this should work:
transform.up = target.position - transform.position;
If not this will work:
tranform.rotation = Quaternion.FromToRotation(transform.up, target.position - transform.position) * transform.rotation;
Just set transform.up
so that it points on your target (I’m assuming the sprite is facing upwards when it’s not rotated)
public void LookAt2D(Transform target) {
transform.up = target.position = transform.position;
}
You can’t use LookAt()
in 2D, because LookAt()
causes the transform’s forward vector to face the target, but in 2D games your sprite faces up by default, not forward, so setting the forward vector to look at the target will do no good.