Limit Rotation On A Rigidbody?

Hi, I was wondering how I’d go about limiting a rigidbody game object’s rotation on the X axis so that it can’t go past 45 degrees up or down.

Thanks!

Use a script then. But the script apply the limit not the rigidbody. A rigidbody only gives it weight and physical feature. So you would need something like. Anyways you can just freeze it’s rotation if you don’t want it to move if not try something like this.

function Update()
{
    if (transform.eulerAngle.x > 45)
    {
        transform.eulerAngle.x = 45;   
    }
    else if (transform.eulerAngle.x < 0)
    {
        transform.eulerAngle.x = 0;
     }
}

I’m getting “Cannot modify a value type return value of ‘UnityEngine.Transform.eulerAngles’. Consider storing the value in a temporary variable.”

Okay. Sorry that was untested. :c

var angleX : float;
function Update()
{
    angleX = transform.eulerAngles.x;
    if (angleX > 45)    
    {        
        angleX = 45;       
    }    
    else if (angleX < 0)    
    {       
         angleX = 0;     
    }
    transform.eulerAngles.x = angleX;
 }

Try this perhaps.

Still doesn’t work. I get the same error on the last line, so I took it out.

Also, I’ve noticed that rotation on other axis’ affects the outcome of the X euler angle.

i thought you said x axis? Anyways care to give more details on what you are doing? Sorry for posting bad codes. I want to know what you are going to do. :smile:

Hi, thanks.

I’m making a space ship game. The ship can move forwards, reverse, rotate around the Y axis, and I want it to have limited 45 degree rotation on the X axis both directions.

Ahh. Icic. I think you might need to use Clamp to clamp values. Unity - Scripting API: Mathf.Clamp

I am thinking you might do something like,

private var angleX : int = 0;
function Update () 
{
    angleX = Mathf.Clamp(angleX, 1, 45);
    transform.eulerAngles.x = angleX;
}

This is only a demo of how clamp works. :smile:

Not sure if it works. You can post ur codes here so that i can see.

Somebody from the IRC told me to use Mathf.Clamp, but I wasn’t sure how to implement it.

Anyway, I’ve got the same error I always get with “transform.eulerAngles.x = angleX;”: “Cannot modify a value type return value of ‘UnityEngine.Transform.eulerAngles’. Consider storing the value in a temporary variable.”

Thats really weird. It runs fine on my computer though. Are you by any chance using C#?

Yeah, I’m using C#.

Edit: Hmm, this says that setting euler angles separately causes drift.

Here you go.

using UnityEngine;
using System.Collections;

public class ClampExample : MonoBehaviour
{
    private int angleX  = 0;
    void Update () 
    {
        angleX = Mathf.Clamp(angleX, 1, 45);
        transform.eulerAngles = new Vector3(angleX, transform.eulerAngles.y, transform.eulerAngles.z);
    }
}

Hmm, now the Game Object won’t rotate on the X axis at all.

Edit: It moves very slowly, then all of a sudden it rotates around the X and Y axis’.

if you use c# you cant (for some strange design reason) assign a variable to a vectorcomponent. you must assign the complete vector instead:

Vector3 tempvect = transform.eulerAngles;
tempvect.x = 35.876f;
transform.eulerAngles = tempvect;

this may also apply in other cases so remember the error message and the solution.

it may mess up when you alter the values directly. better use quaternion - rotate functions for such stuff.

What would that look like?

Edit: Ok, I’ve got something here.

using UnityEngine;
using System.Collections;

public class Physics : MonoBehaviour {
	void FixedUpdate () {
			float angleX = 0.0F;
			float tempvect = transform.eulerAngles.x;
			
			angleX = Mathf.Clamp(tempvect, 1.0F, 45.0F);
			Quaternion quatAngle = Quaternion.AngleAxis(angleX, Vector3.right);
			rigidbody.rotation = quatAngle;
			
		}
	}
}

It works, as it stops at a 45 degree angle. But as soon as it goes below 1 degrees, it snaps back to 45 degrees. I assume it’s working correctly as it’s coded, but I want it to also stop at 315 degrees.

the Vector3.Angle Function does not return the direction (+ or -). look here: http://forum.unity3d.com/threads/13785-Getting-the-angle-between-2-objects especially the post of podperson.

you can put a negative angle into the rotate function so make a case decision.

c# cannot access member variables directly, js can.

So if you’re going .transform.position.x = 10; this works in js, however, in c# you’ll need to make a vector3 and apply it like so: transform.position = myVec;

Hey guys, I’m still having trouble with this. Basically, I have a spaceship and when I add torque on X, I don’t want it to exceed 45 degrees up or down.

try something like this…

float rotationX = transform.localEulerAngles.x;

if(rotationX > 180  rotationX < 315)
{
 transform.localEulerAngles = new Vector3(315, transform.localEulerAngles.y, transform.localEulerAngles.z);  
}
else if(rotationX > 45)
{
 transform.localEulerAngles = new Vector3(45, transform.localEulerAngles.y, transform.localEulerAngles.z);
}
3 Likes

Also you probably want to do thing like this in LateUpdate…

1 Like