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
0
Question by Ezeuz · Mar 26, 2020 at 03:32 AM · rotationphysicsquaternion

Object facing a direction while maintaining rigidbody physics.

I gave up looking the solution to this myself, hundreds of tabs, Q&A, etc for something that's intuitively easy but difficult in reality lol.


All in all, what I wanted:

  • Physical object (player) facing a certain direction (in this case, mouse location).

  • Object still affected by physics (free-fall, tilted when on top of a ramp, stumble when hitting a low-walls, etc).

  • Thus, object is rotated along the Y-axis to follow a certain direction, but the Z and X-axis are set free to follow physics.


What I've tried:

  • Accessing eulerAngles & increment the Y-axis - Works perfectly as intended until it doesn't. As stated in the docs, superbly strange behavior appears. In my case, my object suddenly only rotate in one direction after I hit it to a wall, slower/faster rotation speed, etc. Basically, the eulerAngles just can't be incremented normally (1+1 isn't 2 anymore).

  • Using transform.Rotate(), AngleAxis(), or any other relative rotational method - Works good but jittery. "Programatically" shouldn't suffer from the eulerAngles problems, but somehow similar problem exists.

  • Similar to the method of using eulerAngles, but using a new variable instead of accessing the eulerAngles values directly. Works great on a flat surface since I rotate it along the Y-axis, but then it'd also lock the X and Z-axis since I don't have access to the current values of them both, making it flat even on a slope.


A simple snippet of the code. The angle variable is giving the world-space Y-axis angle for the object to face the mouse.

         // Get the Screen positions of the object
         Vector2 objOnScreen = Camera.main.WorldToViewportPoint(transform.position);
         // Get the Screen position of the mouse
         Vector2 mouseOnScreen = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);
         // Get the angle between the points (absolute goal) = right (target) - left
         float angle = -AngleOffset(Angle2Points(objOnScreen, mouseOnScreen), -90.0f);
 
         // Rotation instant
         transform.rotation = Quaternion.Euler(new Vector3(0f, angle, 0f));

My real code is more complex than this where the rotation is gradual/incremental rather than instant. But functionality wise, this is very similar. This snippet in particular illustrate the third option in the "what I've tried". Modifying it a little

         transform.rotation = Quaternion.Euler(new Vector3(rot.x, angle, rot.z));

with rot is transform.eulerAngles, would give you the first option in that list.


Hence the ultimate question: How to achieve what I wanted without reliance on reading eulerAngles (writing is ok)?

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

1 Reply

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

Answer by Ezeuz · Mar 26, 2020 at 12:34 PM

Okay, it seems I found the main culprit for the 2nd solution in the "what I've tried" list. I tried to lower my angular velocity (or the angle incrementation) and hit the left side of my player to a wall at high speed, then it rotated CCW so fast. Turns out now my player is rotating in one direction only which is CCW, faster when I asked to turn CCW and only get a tad bit slower when I ask it to turn CW. In conclusion:

.

The rigidbody rotation physics is opposing my rotational input.


The most stable solution to this is to prevent the Y-axis rotation in the rigidbody setting. Further implementation would mean I should turn it on or off on the fly, depending on the impact my player get. Something like when you add ragdoll to your human player, turn it off all the time when you're controlling, but turn it on when it's falling or dying.

Second option would be to use AddTorque(), but it's going to be a tad bit more finnicky, as I'd need something like PID. Good for simulation, but don't think it's the right fit for game.

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

224 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rigidbody wont rotate in the other direction. 0 Answers

How to apply parent based rotation? 1 Answer

Constant 3D rotation (torque, angular acceleration, etc) GLOBAL to object in space? 1 Answer

transform.Rotate and Rigidbody.MovePosition 1 Answer

Problems with rotating an object using angular velocity. 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