I’m trying to rotate an object to align with a vector and when I originally searched, I seemed to find something that worked in one instance but it doesn’t work in another. So I’m hoping to get some help getting this to work with my other case.
I have 2 points that are moving away from each other and want to align my object so it faces along the line they form. When I spawn the object, I use this code to get its starting rotation, but if I continue to update the 2 points’s positions and rerun the same code, the object just spins in place very fast. What I’m using is
Vector3 tarVec = point2 - point1;
Quaternion facing = gameObject.transform.rotation;
gameObject.transform.rotation = Quaternion.LookRotation(Vector3.RotateTowards(gameObject.transform.forward, tarVec.normalized, float.MaxValue, float.MaxValue)) * facing;
This works exactly as I hoped when I first spawn my object, but as I modify point1 and point2 to move away from each other, the gameObject just spins like crazy.
Also, if I use OnDrawGizmos to draw a line between point1 and point2, I can see that they are moving apart exactly as I expect. So it’s not like these points are going haywire.