Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by bfbc2jb · Jan 01, 2013 at 10:28 AM · rotationjavascriptrigidbodytransformtilt

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Jan 01, 2013 at 11:02 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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 !

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bfbc2jb · Jan 01, 2013 at 10:58 AM 0
Share

so AA just means the angle of x right?

avatar image Fattie · Jan 01, 2013 at 11:00 AM 0
Share

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.

avatar image bfbc2jb · Jan 01, 2013 at 11:57 AM 0
Share

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

avatar image Fattie · Jan 01, 2013 at 12:02 PM 0
Share

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

avatar image Fattie · Jan 01, 2013 at 12:04 PM 0
Share

Also!

you cannot use "local" and "relative". look at the world angles, and apply world torque.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges