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 SlamKoder · Dec 09, 2013 at 04:23 PM · rotationrigidbodyvelocity

Bending physics - turning rigidbody.velocity 90 degrees

Hi Guys and Girls.

I've been playing around with unity for a short while now and I love it! So I decided to do a simple race game to speed up my learning. So far I have been dissecting the racetrack demo from unity, and it works real well. But I wanted to do a little something extra. So I made a "batman harpoon style" 90 degree turn animation, where I animate the car drifting and turning hard 90 degree right or left, depending on user input.

Once the animation is done, my car has a new heading that is off by +- 90 degrees, but apparently my rigidbody is still heading in the old direction. And so at higher speeds, the car jumps all over the place. I have tried using several code snippets I found on the forums. And what I have found works the best, goes somewhat like this:

     OldSpeed = rigidbody.velocity;
     float sin = Mathf.Sin(grader);
     float cos = Mathf.Cos(grader);

     float tx = OldSpeed.x;
     float tz = OldSpeed.z;
     OldSpeed.x = (cos * tx) + (sin * tz);
     OldSpeed.z = (cos * tz) - (sin * tx);

     rigidbody.isKinematic = true;
     // Do animation
     // Wait for animation to finish (non blocking ofc.)

     rigidbody.isKinematic = false;
     rigidbody.velocity = OldSpeed;

But this only works to some extent. At higher speeds, the problem still persists. And I honestly suck at mathematics, so figuring this one out by myself is just not going to happen. So I'm hoping that there is a way I can rotate my rigidbody velocity 90 degrees around the Global Y axis. So that my car will keep going straight after the animation?

Comment
Add comment
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

2 Replies

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

Answer by robertbu · Dec 09, 2013 at 04:44 PM

This will rotate the velocity 90 degrees:

 rigidbody.velocity = Quaternion.AngleAxis(90.0, Vector3.up) * rigidbody.velocity;
Comment
Add comment · Show 2 · 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 SlamKoder · Dec 10, 2013 at 06:30 AM 0
Share

Thank you! This did indeed fix my problem. :)

avatar image SlamKoder · Dec 10, 2013 at 06:53 AM 0
Share

Problem solved!

Such a "simple" line of code, and to think of all the time I used looking trough much more advanced examples. And this is all it took. :)

I wish there was a formulary, along the lines of a physics or mathematics book. Where things was listed according to use, area of subject or something like that.

The online documentations that comes with unity is great - if you know what to look for. But being a newb at this, I've used countless hours looking trough the documentation, reading the forum and answer section.

Of course I've learned a lot along the way, but I have wasted a lot of time doing so.

Anyways, thanks for helping both of you.

avatar image
1

Answer by Owen-Reynolds · Dec 09, 2013 at 04:47 PM

That looks about correct, assuming grader is computed correctly. Even if you know the cos/sin math, easy to mess-up conversions, signs, forget to normalize... . I'm guessing some not-shown part isn't quite right. But, here's rotation using Unity's tools (which are really just game tools):

 Vector3 oldDir = rigidbody.velocity;
 // freeze and turn over many frames
 rigidbody.velocity = Quaternion.Euler(0,90,0)*oldDir;

Rotation (90) is in clockwise degrees. This wastefully saves and rotates the y speed, but it's easier to read and, and not run often enough to care.

Might also try rotating the angularVelocity -- otherwise if you were tipping back before the turn, it would change into a roll. But no need if movemement is all "flat."

Comment
Add comment · Show 2 · 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 SlamKoder · Dec 10, 2013 at 06:37 AM 0
Share

Sorry about that, grader is of course degrees to turn. Its Danish btw. :)

Thanks for a great answer. As you write, your solution works as long as there is no roll. But as I discovered, the car apparently rolles sometimes, going into a turn. And then strange things happen. I will try the angularVelocity, and see what results I get.

avatar image Bunny83 · Dec 10, 2013 at 11:00 AM 0
Share

And that's your problem ;) in almost all program$$anonymous$$g languages the trigonometric functions work in radians and not degrees.

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Sin.html

The example on the page is, again, a very bad example. They should have used $$anonymous$$athf.PI/2 (which would be 90°) ins$$anonymous$$d of 3 which would be some strange angle in degree

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

18 People are following this question.

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

Related Questions

Rotating a moving object 0 Answers

Rigidbody Disable Velocity/Movement? 1 Answer

A way to kill rotation made by rigid body pysics? 1 Answer

Rigidbody slowing right down when colliding only when I'm rotating the velocity to always be going forward 1 Answer

Adjusting Roll and Pitch with Velocity 0 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