Force to rotate 180 passing through 90º, not -90º

This may be a little wierd. If I want to rotate an object 180, there are 2 ways Unity can handle this, and randomizes the direction. Sometimes it goes from 0->90->180 or sometimes 0->270->180.

Is it possible to force one direction always? ie 0-> 90 → 180.

Suggesting fixes for your code would be easier with a relevant snippet of it available to comment on.

Sure!

It’s basically a 2D platform game:

After casting a raycast to a plane and getting the position of the mouse cursor (point variable), the player should turn right or left depending if the cursor is at player’s right or left.

var bodyPos = point;
bodyPos.y = body.position.y;

var bodyDir : Quaternion = Quaternion.LookRotation(bodyPos - body.position);

if (bodyDir != Quaternion.identity)
{
     var speed : float = 7 * Time.deltaTime;
     body.rotation = Quaternion.Slerp(body.rotation, bodyDir, speed);
}

This code works fine, but I have no control of the rotation, and sometimes the player turns giving it’s back to the camera, and other times facing the camera.

I worked very hard to create a little cute bot in Blender as the player, and would be good if he could show his face when turning right or left, instead of showing his back :wink:

Thanks!

This code pretty much replicates what you have:

// Given: Vector3 point, float rotationSpeed, float minRotation

point = new Vector3 (point.x, transform.position.y, point.z);
	// Make sure we're operating in the same plane
	
float angle = Vector3.Angle (transform.forward, point - transform.position);
angle = angle > 180.0f ? angle - 360.0f : angle;
	// Get the angle in +/- where abs(angle) <= 180

if (Mathf.Abs (angle) > minRotation)
// Only rotate if we need to rotate more than minRotation
{
	transform.RotateAround (transform.position, Vector3.up, angle * Time.deltaTime * rotationSpeed);
}

To force the rotation to always go clockwise, remove the line just before the “// Get the angle in…” comment. To force it to always go counter-clockwise, negate angle when using it.

Note the use of minRotation in stead of your Quaternion.identity setup. Since we’re dealing with floating point here, checking for equality is never a good idea.

AngryAnt, this is the second I owe you. Thanks, tweaked some code and works perfect!