- Home /
limiting rotation of a rigidbody in C#
so... I have been fighting with this for a while. I am new to unity so please excuess my complete ignorance.
this is a "space" flying game. simple, no gravity. The controls work the way I want them to because I have a script that I found and suited to my needs, works great except I want to limit the "roll" of my object say between 45 degrees and -45 degrees.
I thought the line "Mathf.Clamp(AddRot.z,-45,45);" would solve it but my limited understanding is hindering me.
I think it is because the rotation works between 0 and 360, but I am not sure how to tell my object that it can only rotate between 45 and 315.
Heck I am probably completely off my rocker and using it incorrectly and in the wrong place.
please help. this is what my flyscript.cs looks like.
using UnityEngine;
using System.Collections;
public class flyscript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
public float AmbientSpeed = 100.0f;
public float RotationSpeed = 200.0f;
void Update()
{
Quaternion AddRot = Quaternion.identity;
float roll = 0;
float pitch = 0;
float yaw = 0;
roll = Input.GetAxis("roll") * (Time.deltaTime * RotationSpeed);
pitch = Input.GetAxis("pitch") * (Time.deltaTime * RotationSpeed);
yaw = Input.GetAxis("yaw") * (Time.deltaTime * RotationSpeed);
AddRot.eulerAngles = new Vector3(-pitch, yaw, -roll);
Mathf.Clamp(AddRot.z,-45,45);
rigidbody.rotation *= AddRot;
Vector3 AddPos = Vector3.forward;
AddPos = rigidbody.rotation * AddPos;
rigidbody.velocity = AddPos * (Time.deltaTime * AmbientSpeed);
}
}
AddRot.eulerAngles = new Vector3(-pitch, yaw, -roll);
$$anonymous$$athf.Clamp(AddRot.z,-45,45);
change the above to the code below, not sure if this is what you wanted. i am assu$$anonymous$$g you want to stop the role going above 45 or less than -45. its a simple solution
if (roll>45)
{
roll = 45;
}
else if (roll<-45)
{
roll = -45;
}
AddRot.eulerAngles = new Vector3(-pitch, yaw, -roll);
yeah I tried something similar. And I just tried your code, but I get no error messages, but my object still does not stop rotating or "rolling" past a 45 degree angle.
Thank you very much for trying to help me.
I just did a print of my objects rigidbody.rotation.z it never goes above 1 or below -1 ... i guess it is because it never reaches a 45 degree rotation within the update() function. it just keeps rotating a small amount every frame.
i think im confused :(
does you game work with keyboard controls only?
if that's the case. this might not be the optimum solution but will get your game working
// what ever control you want to use for yaw
if (Input.Get$$anonymous$$ey("a"))
{
role-= 0.5f// what ever value you want to use, the higher it is the faster it will be;
}
if (Input.Get$$anonymous$$ey("d"))
{
role+=0.5f;
}
if (Input.Get$$anonymous$$ey("w"))
{
pitch +=1;
}
if (Input.Get$$anonymous$$ey("s"))
{
pitch -=1;
}
if (roll>45)
{
roll = 45;
}
else if (roll<-45)
{
roll = -45;
}
transform.eulerAngles = new Vector3(pitch, yaw,roll);
that is the only thing i have been working with is keyboard controls. i don't use a mouse to control it, but i would like it to be "modular" to potentially work with any type of input.
Answer by Fattie · Oct 02, 2013 at 08:41 AM
Dude - can you simply use Rotate
http://docs.unity3d.com/Documentation/ScriptReference/Transform.Rotate.html
it is unbelievably simple to use. You should very rarely be fooling with the underlying angles.
As a student, you should also study and experiment with
http://docs.unity3d.com/Documentation/ScriptReference/Transform.RotateAround.html
which is incredibly useful. You have to understand the concept of the "rotation axis" to use it, which is exactly what you have to learn here.
Thank you fattie i will use this from now on, the only i just read elsewhere on unity answers link text to only use angles. thank you for pointing me towards the right goal. I will continue to work on it.
Answer by Acr1m · Oct 02, 2013 at 02:43 AM
I think your problem is that you are multiplying your "AddRot" value (which is essentially a deltaRotation) after you've clamped it, when you need to clamp it AFTER the addition of the rotation. Cuz as it is, it's clamping, then multiplying the AddRot with the -45 and the 45.
// Mathf.Clamp(AddRot.z,-45,45);
rigidbody.rotation *= AddRot;
if(transform.localRotation.z < -45)
transform.localRotation.z = -45;
else if(transform.localRotation.z > 45)
transform.localRotation.z = 45;
Answer by raimon.massanet · Oct 02, 2013 at 01:31 PM
As Fattie said, you should use Transform.Rotate
to rotate your object, instead of messing with its euler angles.
However, your code is not working as expected because your are not clamping your rotation value, because your are not assigning it.
You need to assign the result of Mathf.Clamp(AddRot.z,-45,45)
somewhere and apply it.
I need to figure out how to use mathf.clamp before i can apply it anywhere. thank you for your time. I will continue to work on it.
Your answer
Follow this Question
Related Questions
Can i Move/Rotate triggers without Rigidbodies? And other collider questions. 3 Answers
Pysics not working as expected 1 Answer
Oculus VR Wrist Rotation Relative to Arm 0 Answers
Objects Instantiating at wrong position 3 Answers
Help making orbiting planets,How to rotate around the objects with the largest mass? 0 Answers