Annoying smooth rotation jitter bug

Hi all,

I’m working on a script that does semi-random movement of animals like seagulls, whales and so on. I made it so, that the entity rotates smoothly to it’s next target (with Vector.Lerp). For some reason, it sometimes starts jittering around. I assume it’s the Vector3.lerp that does this, for some reason. When it does start to spin, it’s almost always straight up or straight down.

I tried to solve it by changing some stuff allready, so take a look at the list below before posting.

  1. Do the Rotation in FixedUpdate instead of Update.
  2. Take out the Time.deltaTime component in the Vector3.Lerp function.
  3. Rotate the entity a bit after generating a new Destination.
  4. Null Rotation and SmoothRotation after generating a new Destination.
  5. After using LookAt and saving the rotation, set the rotation back to what it was before.
  6. Muck about with the third component of the Lerp function.
  7. While doing the above, keep switching between Lerp and Slerp.

None of the above worked. Using Lerp instead of Slerp does help.

You can see the script in action here.

The script:

using UnityEngine;
using System.Collections;

public class Static_RandomMovement : MonoBehaviour {
	
	//////////////////////////////////////////////
	//	Script:		Static_RandomMovement.cs	//
	//	Project:	Pirate Project				//
	//	Scripter:	De Yuang Ann Wijning		//
	//											//
	//	Description:							//
	//	Handles semi-random movement of animals	//
	//	such as whales and seagulls.			//
	//	Sometimes fucks up and jitters around.	//
	//////////////////////////////////////////////
	
	public bool ElectNewPos 		= true;			// Request new Destination
	private Vector3 Destination 	= Vector3.zero;	// Point the entity moves towards
	private Vector3 SmoothRotation 	= Vector3.zero;	// Smoothed out Rotation
	private Vector3 Rotation 		= Vector3.zero;	// Raw Rotation
	public Vector3 Offset 			= Vector3.zero; // Entity's Offset
	public bool CustomOffset 		= false;		// Does the Entity have a costum offset?
	public bool FlatPlane 			= false;		// If you turn this on, the entity doesn't change height.
	public float MinimumDistance 	= 2F;			// Minimum distance the new Destination has to be from the current position
	public float xFarLimit 			=-10F;			// Smallest X
	public float xNearLimit 		= 10F;			// Biggest X
	public float yFarLimit			=-10F;			// Smallest Y
	public float yNearLimit 		= 5F;			// Biggest Y. If the Entity is a sea creature (such as a whale), make sure this is lower than the sea itself.
	public float zFarLimit 			=-10F;			// Smallest Z
	public float zNearLimit 		= 10F;			// Biggest Z
	public float Speed 				= 10F;			// Entity's movement speed
	public GameObject TestObject 	= null;			// Cube Target
	
	// Take this out once jittering has stopped
	public int SuccesCount			= 0;			// How many times has the seagull succesfully reached the target?
	public int FailCount			= 0;			// How many time did it fuck up above task?
	
	void Start(){
		
		Rotation = transform.eulerAngles;
		if(xFarLimit>xNearLimit){
			xFarLimit = xNearLimit;	
		}
		if(yFarLimit>yNearLimit){
			yFarLimit = yNearLimit;	
		}
		if(zFarLimit>zNearLimit){
			zFarLimit = zNearLimit;	
		}
		if(CustomOffset == false){
			Offset = transform.position;
		}
		GenerateNewPos ();
	}
	
	void GenerateNewPos(){
		
		ElectNewPos = true;
		Destination = Vector3.zero;
		SmoothRotation = Vector3.zero;
		Rotation = Vector3.zero;
		
		if(FlatPlane == true)
			Destination = new Vector3(Random.Range (xFarLimit, xNearLimit), transform.position.y, Random.Range (zFarLimit, zNearLimit));
		else
			Destination = new Vector3(Random.Range (xFarLimit, xNearLimit), Random.Range (yFarLimit, yNearLimit), Random.Range (zFarLimit, zNearLimit));
		
		//Failsave
		if(Vector3.Distance (transform.position, Destination) < MinimumDistance){
			print ("Static_RandomMovement.cs notice: Distance is too close, generating a new destination point.");
			GenerateNewPos ();
		}
		else{
			ElectNewPos = false;
			TestObject.transform.position = Destination;
			Move ();
		}
	}
	
	void FixedUpdate () {
	
		if(ElectNewPos == true || Vector3.Distance(transform.position, Destination) < 1F){
			SuccesCount ++;
			GenerateNewPos ();
			print ("Static_RandomMovement.cs notice: Destination reached. Generating new roam target.");
		}
		else if(transform.position.y < yFarLimit - 2F || transform.position.y > yNearLimit + 2F){
			FailCount ++;
			print ("Static_RandomMovement.cs Alert: Torpedo Alert!");
			transform.LookAt(Destination);
			transform.Translate (Vector3.forward * Speed * Time.deltaTime);
		}
		else{
			Move();
		}
	}
	
	void Move(){
		
		// Calculate Rotation
		Rotation = transform.eulerAngles;
		transform.LookAt(Destination);
		SmoothRotation = Vector3.Lerp(Rotation, transform.eulerAngles, Speed * 0.01F);
		
		// Rotate
		transform.eulerAngles = SmoothRotation;
		
		// Move
		transform.Translate (Vector3.forward * Speed * Time.deltaTime);
	}
	
	void OnGUI(){
		
		// Take this out once jittering has stopped
		GUI.Label(new Rect(Screen.width * 0.88F, Screen.height * 0.88F, Screen.width * 0.11F, Screen.height * 0.11F), "Succes Count: " + SuccesCount);
		GUI.Label(new Rect(Screen.width * 0.88F, Screen.height * 0.77F, Screen.width * 0.11F, Screen.height * 0.11F), "Fail Count: " + FailCount);
		
	}
}

Thank you for reading.

Probably related to Gimbal Lock. You should’nt use euler angles rotation interpolation because of that. If you switch to Quaterion.Lerp for your rotation, I think you will solve your problem