Rotation in Unity3D

William picture William · Feb 16, 2012 · Viewed 35.9k times · Source

This is a simplified code from what I'm trying to do:

var angle = 1.57;
if ( this.transform.rotation.y > angle ){
  this.transform.rotation.y--;
} else if ( this.transform.rotation.y < angle ){
  this.transform.rotation.y++;
}

I'm used to code in AS3, and if I do that in flash, it works perfectly, though in Unity3D it doesn't, and I'm having a hard time figuring out why, or how could I get that effect.

Can anybody help me? Thanks!

edit:

my object is a rigidbody car with 2 capsule colliders driving in a "bumpy" floor, and at some point he just loses direction precision, and I think its because of it's heirarchical rotation system.

(thanks to kay for the transform.eulerAngles tip)

Answer

Kay picture Kay · Feb 16, 2012

transform.rotation retrieves a Quaternion. Try transform.rotation.eulerAngles.y instead.