Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by CheshireCat · Jan 28, 2010 at 07:40 PM · rotationrigidbodyvector

How might I rotate rigidbody momentum?

Hi,
I currently have a rigidbody spaceship that is controlled by the player. I have assumed that the spaceship should be a non-kinematic rigidbody because I want it to bounce and rotate during collisions. Please let me know if this is wrong.

The player can use the up and down arrow keys to give the rigidbody momentum (the keys enable and disable thrust). This acts like acceleration, by calling rigidbody.AddRelativeForce . This was exactly what I wanted.

I then went and made it so the player could turn the rigidbody, by implementing rigidbody.AddRelativeTorque . This did not achieve the desired effect, as the directional vector already in place caused the rigidbody to drift.

My question is: how do I rotate this rigidbody such that momentum rotates with the object? More precisely, is there a function that will do it for me, or should I use the math component to manually rotate the force vector on the rigidbody?

Thanks,
Andrew

EDIT:
Special thanks to Brian Kehrer and Horsman for putting me on the right track. I slightly modified their solution, by multiplying the original vector magnitude by the directional vector, rather than the transform vector, which yielded all sorts of crazy results.

rigidbody.velocity = Vector3.forward * rigidbody.velocity.magnitude;

Thanks for the help!

Comment
Add comment · Show 2
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 TinyUtopia · May 27, 2010 at 07:09 AM 0
Share

Sorry to be dense, but where do you enter the rotation input for this? i.e. if you're using:

turning = Input.GetAxis ("Horizontal") ;

avatar image Pria · Jun 28, 2010 at 11:44 AM 0
Share

I have a kinematic rigidbody..and I want to do rotate rigidbody in momentum and stop it then. How is that possible??

4 Replies

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

Answer by Horsman · Jan 29, 2010 at 12:45 AM

It is not physically acurate to do this, of course, but you can acheive it easily by rotating the current velocity of the ship.


Vector3 velocity = rigidbody.velocity;
rigidbody.velocity = Vector3.zero;
rigidbody.velocity = transform.forward * velocity.magnitude;
Comment
Add comment · Show 1 · 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 perttikontio · Jan 25, 2019 at 12:38 PM 1
Share

Basically this ought to do the trick:

 rigidbody.velocity = transform.forward * rigidbody.velocity.magnitude;
avatar image
3
Best Answer

Answer by Brian-Kehrer · Jan 28, 2010 at 08:58 PM

It's not physical, but you could just rotate the velocity vector and reapply it. This typically results in more intuitive, better feeling controls.

EDIT The easiest way to do this is maintain a transform as the current intended velocity vector. Rotate the transform when the player rotates, and then call Transform.forward.

Multiply that by the Rigidbody.velocity.magnitude and reassign that to Rigidbody.velocity.

Comment
Add comment · Show 1 · 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 CheshireCat · Jan 28, 2010 at 09:19 PM 0
Share

That is what I'd meant when I mentioned manually rotating the vector. I wanted to make sure there wasn't a function that would do this for me ins$$anonymous$$d.

On the other hand, I have absolutely no idea what the best way to rotate a vector is given the API (I'm new here!).

avatar image
0

Answer by paranoidray · Feb 29, 2016 at 04:32 AM

I wanted to model a bird flying. I am using this in the mouse controller:

 transform.Rotate(Vector3.up, x * xSpeed);
 rigidBody.velocity = Quaternion.Euler(0, x*xSpeed, 0) * rigidBody.velocity;

where x is

 Input.GetAxis("Mouse X");

In the WASD script I use:

  m_Rigidbody.AddForce(f * 10.0f * Camera.main.transform.forward + s * 10.0f * Camera.main.transform.right);

f is forward button and s is strafe button press

     int f = Input.GetKey(KeyCode.W) ? 1 : Input.GetKey(KeyCode.S) ? -1 : 0;
     int s = Input.GetKey(KeyCode.D) ? 1 : Input.GetKey(KeyCode.A) ? -1 : 0;
Comment
Add comment · 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
0

Answer by Virepri · Aug 16, 2018 at 08:41 PM

I actually solved this by getting the change in the angle I'm trying to rotate on.

For reference, rb is rigidbody, pc is my camera script. prevRot is the rotation before the mouse moved.

 rb.velocity = Quaternion.Euler(0, transform.eulerAngles.y - pc.prevRot, 0) * rb.velocity;
Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Aligning a rigidbody smoothly with the perpendicular of a vector angle 0 Answers

Object not rotating when the associated trigger is triggered 1 Answer

How to tilt the gameobject based of Input.Acceleration? 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to create a damped harmonic oscillation? 1 Answer


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