weird glitching when interpolating rotation

ok, so here’s what’s going on. Essentially, I have an object that I want to rotate and look at another object as it gets closer. I want the rotation to interpolate based on distance (e.g. when it’s 12 units away it’s at its start rotation, when it’s 6 units away it’s halfway rotated towards the target, at 0, it’s fully pointing at the object).

Also, I wanted it to do it one axis at a time. So when it’s within the first third of range(if range is 12, this is from the distance 12 to 8), it rotates on the x axis, in the middle section of range (8 to 4), it does the y axis, and then finally within the final range (4 to 0) it does the z axis.

I got this all working perfectly, except the rotations randomly spazz out. The object will be rotating, and then suddenly flip before continuing. I’m having trouble recreating, so I haven’t been able to figure what’s causing it.

(Note, if I did all 3 rotations at the same time it would spazz out as well, I think it’s something to do with interpolation)

Here’s my script, I’d appreciate any suggestions:

public class EndRuneLookAt : MonoBehaviour {

	float smoothTime = 0.7f;

	Transform otherRune; //what this object will look at
	Transform _transform; //cache transform

	float distanceToLookAt = 10f; //within what range to start rotating
	Vector3 startRotation; //remember start rotation

	Vector3 velo; //velocity for smoothdamp
	
	void Start () 
	{
		_transform = transform;
		string ourName = transform.name;
		startRotation = _transform.localEulerAngles;
		GameObject[] allRunes = GameObject.FindGameObjectsWithTag ("EndRune"); //find other object

		for(int i = 0; i < allRunes.Length; i++) //make sure it isn't self
		{
			if(allRunes*.name != ourName)*
  •  	{*
    

_ otherRune = allRunes*.transform;_
_
break;_
_
}_
_
}_
_
}_
_
Vector3 t; //for interpolation*_
* void Update ()*
* {*
* //current distance, subtracting 3 makes it seems like they are closer than they are*
* float curDistance = Vector3.Distance (transform.position, otherRune.position) - 3f;
_
curDistance = Mathf.Clamp (curDistance, 0, 100); //prevent from being negative*

* //calculate goal rotation*
* Vector3 relativePos = otherRune.position - transform.position;*
* Quaternion newRotation = Quaternion.LookRotation(relativePos);*

* float segment = (distanceToLookAt / 3f); //distance between rotation axis*
* Vector3 goalRotation = newRotation.eulerAngles;*
* Vector3 curRotation = _transform.localEulerAngles;*

* if(curDistance < distanceToLookAt) //if within first 3rd of range*
* {*

_ float goal = 1 - ((curDistance - (2f * segment))/(distanceToLookAt - (2f * segment)));_
* t.x = Mathf.SmoothDamp(t.x, goal, ref velo.x, smoothTime);*
* curRotation.x = Mathf.LerpAngle(startRotation.x,goalRotation.x,t.x);*

* }*
* if(curDistance <= distanceToLookAt - segment) //if within 2nd segment of range*
* {*
* float goal = 1 - ((curDistance - segment)/(distanceToLookAt - segment));*

* t.y = Mathf.SmoothDamp(t.y, goal, ref velo.y, smoothTime);*
* curRotation.y = Mathf.LerpAngle(startRotation.y,goalRotation.y,t.y);*

* }*
* if(curDistance <= segment) //if within final segment*
* {*
* float goal = 1 - (curDistance/segment);*
* t.z = Mathf.SmoothDamp(t.z, goal, ref velo.z, smoothTime);*
* curRotation.z = Mathf.LerpAngle(startRotation.z,goalRotation.z,t.z);*

* }*

* transform.localEulerAngles = curRotation;
_
}*

}

You’re likely running into Gimbal Lock. Try using Quaternion.Euler to set your rotation instead of directly modifying the localEulerAngles of your transform.