- Home /
Question by
JesseFox · Feb 16, 2015 at 10:43 AM ·
scripting problemphysicsaddtorque
Rotating a rigidbody using AddTorque, need help!!!
Using c# I'm trying to get a cube when a horizontal key is pressed to rotate to the left/right on its z axis to a desired location ( red lines in figure a/b/c ) and when no keys are pressed the cube rotates back to upright position ( the green lines ) I'm trying to use AddTorque but I can't seem to figure out a solution that works, if anyone could help me with this problem or give me a guide of where to go next that would be much appreciated, I want to implement this code onto a bike or any other two wheeled vehicle where it could simulate the rider leaning the bike over.
finalpictureexample.png
(83.3 kB)
Comment
Answer by giulio-pierucci · Feb 16, 2015 at 11:11 AM
May do it with several method. An example:
if(Input.GetKey(KeyCode.LeftArrow))
transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);
else if(Input.GetKey(KeyCode.RightArrow))
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
else if
{
.....
Compute a cross product to check if transform.up is rotated to the left or right the green line.
if left transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime)
if right transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime)
if 0 -> nothing
............
}