# A very simple rotation question

I am getting along leaps and bounds with Unity3D and scripting, but this one is testing me.

Very simple question

I want to check a characters rotation from the vertical on Update and keep it within -10 and 10 degrees from the vertical to stop it from tipping over.

Can anyone give me a quick example applied to a gameObject please ?

Many thanks

I’m just going to put this one out there right now, you don’t mention it, but be careful if you go with a route that restricts via ‘euler angles’, because they just don’t work well for all situations.

As for how to solve this.

Say you have some normal vector off which you want to measure. Lets say Vector3.up in your situation, I’m assuming you’re describing a situation where say you have a top (the toy), and you don’t want to let it fall over, I assume this is the situation you’re describing.

You can measure the angle between the local up, and the global up, to see how far the object is leaning over.

If it’s greater than your max, you can rotate back towards up.

``````float maxAngle = 10f;

var a = Vector3.Angle(this.transform.up, Vector3.up);
if(a > maxAngle)
{
var v = Vector3.RotateTowards(Vector3.up, this.transform.up, maxAngle, 0f); //get the vector that would be 10 off of up
var q = Quaternion.FromToRotation(this.transform.up, v); //get a rotation from current, to that appropriate up
this.transform.rotation *= q; //rotate by that amount
}
``````

Yes the situation I am describing. My character object is toppling over and I need to prevent it.

Many thanks for the quick reply, I shall give it a go when I am back in project and report back.

Thanks, I tried this but something strange was happening. The angle was being detected but the transforms were doing nothing. Since and a bit more reading, using locked axis on the constraints of RigidBody has solved my immediate need.
But I thank you for the code, I am definitely going to use this to learn about vectors and rotation.

Not sure if I can mark it as the “answer” as I could not get it to work with my model.

Thanks again.

1. my apologies, the Vector3.RotateTowards uses radians, not degrees…
``````var v =Vector3.RotateTowards(Vector3.up, this.transform.up, maxAngle * Mathf.Deg2Rad, 0f);
``````

I often just write these in the browser when I respond. And the fact unity just swaps back and forth between radians and degrees all willy nilly throws me off some times.

1. rigidbodies add a little more complexity to this as well since you have to compensate for the forces being applied

The rigidbody constraints are awesome, since they do all that compensation for you, but unfortunately it doesn’t support ranges.

Otherwise, if you want the range, you’re going to have to get a reference to the Rigidbody and adjust accordingly.

I thought something was up Will give it a try as I would like to see some tipping as a character moves up hill.

If you want to adjust for the forces, here’s an example doing so:

``````    float maxAngle = 10f;

private void FixedUpdate()
{

var a = Vector3.Angle(this.transform.up, Vector3.up);
if (a > maxAngle)
{
var v = Vector3.RotateTowards(Vector3.up, this.transform.up, maxAngle * Mathf.Deg2Rad, 0f);
var q = Quaternion.FromToRotation(this.transform.up, v);
//this.transform.rotation *= q; //rotate by that amount

var rb = this.GetComponent<Rigidbody>();
Vector3 axis;
float angle;
q.ToAngleAxis(out angle, out axis);
rb.angularVelocity = Vector3.zero;