Tracking Object Rotation?

Hi,

I’m working on a game where the player can do flips, I need to find a method of a script to ‘watch’ how many degrees the player has turned, so for example - If, full 360 to the right, then = frontflip. If, full 360 to the left, then = backflip.

But I’m really not too sure where i would start with this…

Any help would be really appreciated!

Thanks.

I would use a different approach: have a float variable curAngle, modify it according to the controls and then set the player rotation to curAngle using eulerAngles. Since you must not set individual eulerAngles components, save its initial value in a variable euler, then modify only the desired axis. In the example below (attached to the player), the player turns around the Y axis:

var curAngle: float = 0.0; // current angle about Y
var turnSpeed: float = 15.0; // degrees per second
private var euler: Vector3; // save the initial euler angles

function Start(){
  euler = transform.eulerAngles; // initialize the euler variable
}

function Update(){
  // modify the curAngle variable using the controls
  curAngle += Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime;
  // set the Y euler angle in modulo 360
  euler.y = curAngle % 360;
  // update the player rotation
  transform.eulerAngles = euler;
}

You can always know the current angle reading curAngle.

EDITED: Ok, if you want to know the current angle about Y, you can just read transform.eulerAngles.y

This will work fine until the eulerAngles x and z are not far from zero: weird y angles may be returned at some points otherwise. The property eulerAngles is just transform.rotation in a 3-axes format; since 3-axes format is redundant (0,180,0 is the same rotation as 180,0,180) many different xyz angle combinations may be returned for the same rotation. Unity tries to return the most logical combination, but when the x, y and z are all non-zero, some weird values may be returned.

Case you have problems with eulerAngles.y, try the simple function below, which returns the current rotation angle around Y - the angle is “extracted” from the quaternion components:

function AngleAboutY(q: Quaternion): float {
  var ang = 2*Mathf.Rad2Deg*Mathf.Acos(q.w*Mathf.Sign(q.y));
  if (ang > 180) ang -= 360;
  return ang;
}

// read the current object rotation with something like this:
  angle = AngleAboutY(transform.rotation);

You can store the euler angles when the movement starts (ex: in your “jump” script), then compare with new angles when the player hits the ground. This hit-check can be an OnTriggerStart event, or simply waiting for its y-speed be equal to zero.