- Home /
Add max Angle to rotation.
Hi, how can i add the max rotation angle to this script?
#pragma strict
var amount = 5.0;
function Awake ()
{
}
function FixedUpdate()
{
if (Input.GetKey(KeyCode.A)) {
rigidbody.AddForce(transform.right * amount);
}
else if (Input.GetKey(KeyCode.D)) {
rigidbody.AddForce(-transform.right * amount);
}
else if (Input.GetKey(KeyCode.S)) {
rigidbody.velocity = rigidbody.velocity * 0.9;
}
}
by maximum rotation angle do you mean to restrict the object rotation to certain amount or the instantiated object
Answer by deltamish · Apr 07, 2014 at 11:54 AM
Hi Inorder for you to restrict an objects rotation you need to know some basics about Vectors and how Unity sees it
and is the object rotates manually from this script or another script ?
here is an simple script that restricts the object rotation on its Z axis with respect to its forward vector at start
public var MaxAngle:float = 180f;
private var CurrentAngle:float =0;
Vector3 ForwardVector;
Quaternion rotation;
function Start()
{
ForwardVector = transform.forward;
}
function Update()
{
}
function LateUpdate()
{
CurrentAngle = Vector3.Angle(ForwardVector,transform.forward);
if(CurrentAngle <= MaxAnlge)
rotation = transform.rotation; //update the changs only if angle is less than max
transform.rotation.x = rotation.x; // set rotation on x axis (which maske the object rotate forward)
}
Note : this is an simple script that restricts the angle on zaxis only if you want it on other axis then you have to write another code or just write the same code for different axis as well .If want any help please ask
sorry but before i don't sow this.. the code that i've posted works with a hinge joint, is your code also good?
what is wrong waht is happening.I havent tested it yet.
Once again is the rotation due to this or other object
I added your script to my, but unity give me errors, so i cannot try it if works.