Qauternion.wtfnow?

So i’m using the following code on an object. I have used this before with out problems but now when the object rotates to 180 degrees from it original rotation it will stop rotating or pause for awhile and the rotate extremely fast to the target.
Only thing I know that could be effecting it is a rigid body. if so, then whats the way around it?(Line 40)

var playerObject : GameObject;
var topObject : GameObject;
var bulletSpawn : GameObject;
var bulletSound : GameObject;

var moveSpeed : float = 3;

var baseRotationSpeed : float = 3;
var topRotationSpeed : float = 3;

var shootTime : float = 4; //In seconds;
private var shootTimer : float;

var hasBeenAlerted : boolean; // Will activated from a trigger.
var canSeePlayer : boolean = false;

var myTransform : Transform;
var playerTransform : Transform;


function Awake () 
{
	playerObject = GameObject.FindWithTag("Player");
	myTransform = transform.parent;
	playerTransform = playerObject.transform;
}

function Update() 
{
	baseMoveFunc();
	topFunc();
}

function baseMoveFunc()
{
	
	if (!hasBeenAlerted)
	{
		//Rotate to player with given time.
		myTransform.rotation.y = Quaternion.Lerp(myTransform.rotation, Quaternion.LookRotation(playerTransform.position - myTransform.position), baseRotationSpeed * Time.deltaTime).y;
		//Move Towards Player at given speed.
		myTransform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
	}
}

Transform.rotation is a 4-dimensional quaternion, and rotation.y is not the y axis (quaternions have 4 elements: x, y, z, and w, none of which directly correspond to euler angle axes). If you ever used something like that before, it was only a coincidence that it worked at all. You have to use the entire rotation, not just one element.

–Eric

With or with out rotation.y it does the same thing. It will stop after 180 degrees. (See below)

 transform.rotation = Quaternion.Slerp(fromRotation, toRotation, rotateSpeed * Time.deltaTime);

If your meaning this is impossible to work then I would of never knew about it. You can use .y.

 transform.rotation.y = Quaternion.Slerp(fromRotation, toRotation, rotateSpeed * Time.deltaTime).y;

Sorry if I may misunderstood you. If there is a way to keep it rotating towards player with smothness (in this case). Then please help me. :wink:

Try using transform.eulerAngles or Quaternion.EulerAngles. Quaternions are pretty complicated and you should avoid working with them if you don’t fully understand how to use them. Euler angles are often a better option.

Actually pretty simple. As people love to create LookRotation, it is not compeletly nessecary. You can simply capture the existing rotation, force the AI to look at the player then lerp the difference. Move him as you need.

// get the players location
Vector3 player = playerTransform.position;
//player.y = myTransform.position.y; // if you were using a CharacterController you would use this to keep the X rotation correct.

// capture the existing rotation
Quaternion rotation = myTransform.rotation;
// force the ai to look at the player
myTransform.LookAt(player);
// Lerp the rotation from the original rotation to the look rotation.
// based off of baseRotationSpeed
myTransform.rotation = Quaternion.Lerp(rotation, myTransform.rotation, baseRotationSpeed * Time.deltaTime);

//Move Towards Player at given speed.
//Only move him if he is close to facing the player.
if(Vector3.Dot(myTransform.forward, player.normalized) > 0.6f)
	myTransform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);

There ya go, all nice and commented. :wink: