- Home /
Anti-Gravity vehicle bounce-back help.
Hello, i am making a game where you control a ufo like Anti-Gravity-vehicle, the problem is, when the saucer hits something and it tilts i want it to smoothly bounce back to its original rotation, ive made a script but there is a problem.
var xRotation:float;
var xForce:float;
function Start () {
}
function Update () {
xRotation=transform.localEulerAngles.x;
if(xRotation==270)
{
xForce=0;
}
if(xRotation>270)
{
xForce=-50;
rigidbody.AddRelativeTorque(xForce,0,0);
}
if(xRotation<270)
{
xForce=50;
rigidbody.AddRelativeTorque(xForce,0,0);
}
}
im testing it on the x axis for. the script relies on transform.localEulerAngle.x/z for rotation detection. but for some odd reason when the craft tilts the positive x the value in the transform.eulerAngles (the one in the inspector) goes up (like normal) but when it tilts negative x the value still goes up, resulting the craft rotating to 0 instead of 270 where its supposed to be, making the crafts front facing up!, what!?? Thanks in advance, really appreciate it.
NOTE - it's not entirely certain you should be using the LOCAL angles
is the spaceship "contained in" something? if so why?
my guess is you should very simply be looking at the rotation, in the real world .. ie, transform.eulerAngles.
Answer by Fattie · Jan 01, 2013 at 10:33 AM
Could this be the problem:
add a line Debug.Log(" the angle is now .. " +localEulerAngles.x);
you'll see that the angle is expressed:
from 0 to 360 degrees
in other words, "355" means "5 left" and "13" means "13 right"
This is always an ongoing huge problem in video games. There are different ways of expressing angles, and you're always needing to change between them!!
In this case, you probably want it more like this:
"-5" means 5 left, and "+13" means 13 right
If that makes sense, all you need to know is how to convert from "0 to 360 style" to "plus and minus style".
Fortunately this is easy to do.
It's basically: if the angle AA is bigger than 180, then AA = ( AA - 360.0 );
So for example if AA is 358, you get 358-360 = minus two. So that's correct.
So FIRST you have to convert to "plus and minus style"
THEN you have to totally delete all your complicated code above regarding 270, etc
THEN you have to write new code, based on the plus or minus style" - heh - which is really easy.
Lemme know if this fixes the woe !
sure, it was just an example variable name.
you would do something like:
temporary .. is a Vector3
temporary = localEulerAngles
"fix" each one of the three elements of temporary
THEN you would perform whatever you want, using temporary.x, etc.
still does same thing as before, no matter which way it tilts in x angle the value still goes down and this time the craft just keeps spinning non stop.
var vect3:Vector3;
var x:float;
var xforce:float;
function Start () {
}
function Update () {
vect3=transform.eulerAngles;
if(vect3.x>180)
x=(vect3.x-360.0);
if(x==-90)
xforce=0;
if(x>-90)
{
xforce=100;
rigidbody.AddTorque(xforce,0,0);
}
if(x<-90)
{
xforce=-100;
rigidbody.AddTorque(xforce,0,0);
}
}
am i doing right? EDIT: Only just saw the comment up top, changed to world angles, still same result though
don't forget X EQUALS ZERO is upright !!!!!!!!!!!!!
add a line of code Debug.Log( localEulerAngles.x );
If x=0 is NOT upright, your game WILL NOT WOR$$anonymous$$.
In Unity, the models must be oriented so that if eulerAngle.x is zero, then the model is sitting upright
Also!
you cannot use "local" and "relative". look at the world angles, and apply world torque.
Your answer
Follow this Question
Related Questions
Rotating Rigidbody properly using AddRelativeForce 0 Answers
transform.LookAt overrides rigidbody rotation 0 Answers
Change object position on trigger enter 3 Answers
Object not rotating 1 Answer
Finding A Rigidbody's Rotation Speed And Direction 2 Answers