Arghh!! Character Controllers have limited x rotation

This is the Unity documentation for Character Controllers:

I wish it said that a GameObject with a Character Controller attached can only be rotated on the x axis from 270 degrees through 360-0 degrees to 90 degrees. You can’t rotate its x axis to 100 degrees, 180 degrees, or 260 degrees, according to a test I just ran. That means your player GameObject with the attached Character Controller can never flip, stand on his head, or do a cartwheel on that axis (unless that’s pure animation that doesn’t require the GameObject to actually rotate).

Here’s the test, in case you want to try it yourself:

function Update() {
    gameObject.rotation.eulerAngles.x += 1; //rotate 1 degree per frame
}

Attach that code to an elongated GameObject so you can see it rotating. Attach a Character Controller to that GameObject too. Then run the scene. When I did it, it only rotated to 90 degrees.

Another test is to set the x rotation of the GameObject to 180 before the scene runs, then run the scene and quickly pause it. If you switch to scene view right then, you should see that the GameObject no longer has a rotation of 180 degrees.

Why did I discover this? Because in my game I have guys running around with Character Controllers attached. When there’s an explosion I programmed them to get flung away and to spin. Spinning them on the y-axis and z-axis worked fine but on the x-axis they’d get stuck and vibrate. Not very realistic-looking.

PS: I sure hope I’m not wrong about this. That would be embarrassing to have written all this if it was just programmer error!

I believe this is the result of quaternion/euler angle conversion. According to the docs you shouldn’t set one element of a eulerAngle separately; all 3 should be set at the same time. Try this:

var xangle = 0;

function Update() {
    transform.eulerAngles = Vector3 (xangle++, 0, 0); //rotate 1 degree per frame 
}

Watch what happens to the rotation values when X goes past 90. Gotta love them quaternions. :wink:

(Not sure if it’s relevant, and you probably already know this, but the character controller collider itself is always upright and can only ever rotate on the Y axis anyway.)

–Eric

Thanks for the reply. That’s wacky!

The thing that bugs me is that this only happens for the X axis. The Y axis and Z axis rotate through 360 degrees no problem. I’ll experiment with it some more.

Whoops!!! Programmer error. Well, not really “error”, but I didn’t read ALL the documentation. (There’s a LOT of documentation!)

This problem isn’t with Character Controllers. It’s because you aren’t supposed to use eulerAngles to set rotations. But then, I thought, how the heck are you supposed to set rotations? According to documentation, don’t use Quaternions “unless you know them inside and out”. (That’s a direct quote. Scared me away from Quaternions.)

But then one of the guys I work with found this code that rotates the GameObject through 360 degrees on the x axis no problem:

function Update() {
	transform.Rotate(Vector3.right * Time.deltaTime * 45);
}