Question by
HoneyBasil · Sep 14, 2021 at 01:16 AM ·
2drotation
Rotate towards an object in 2D
Hey, so I want an object to always rotate towards another, but to smoothly turn towards it over time. At the current moment, I have this
void Update()
{
spear.transform.right = Vector2.Lerp
(spear.transform.position,
player.transform.position - spear.transform.position,
Time.deltaTime * turnSpeed);
}
I figure it would work fine, but it only works if turnSpeed is a high number (>80), and at that point it's too fast. If it's lower than that, the z rotation will flicker, or be stuck between ~-10 to ~10 at lower numbers. I'm sure there's a better way to do this, but I am curious as to why this isn't working, and why it's acting the way it is.
Thanks for the help!
Comment
Best Answer
Answer by HoneyBasil · Sep 14, 2021 at 01:22 AM
Figured it out!
void Update()
{
//testing quats
Vector3 dir =
player.transform.position - spear.transform.position;
Quaternion rot =
Quaternion.LookRotation(Vector3.forward, dir);
spear.transform.rotation =
Quaternion.Lerp(spear.transform.rotation, rot, Time.deltaTime * turnSpeed);
}
Couldn't figure out how to get Quaternions to work in a 2D space before, was always giving me problems. Anyways, I'm still confused as to why my original code acted the way it did.
Your answer