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 Dan 6 · Aug 22, 2010 at 11:07 AM · rotationrigidbodyaddrelativeforce

Unprecise rotation when adding a relative force to a rigidbody

I have a gameObject that is rotated by pressing the a and d keys moved and moved by pressing the right mouse button (adding a relative force to the rigidbody).

I use the following code:

void Update() {

 float rot = Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime;

 transform.Rotate(0, rot, 0);




}

void FixedUpdate() { if (Input.GetButton("Fire2")) { rigidbody.AddRelativeForce(0, 0, forwardSpeed * Time.deltaTime); } }

This works, well, almost!

If the object is only rotated a few degrees, the object does not move forward when a applied a force to it. It's like it rounds the angle of the force to the nearest 10 degrees or so.

In a test, where a added a lot of force to the object on RMB, it looked like it worked fine.

What am I doing wrong? How can I add more precision to the direction of the force.

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 Dan 6 · Aug 22, 2010 at 11:34 AM 0
Share

Just found a similar question here: http://answers.unity3d.com/questions/8302/addrelativeforce-miscalculations But I am not entirely satisfied with the answer as I want to use different physics materials with different frictions.

2 Replies

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

Answer by Dan 6 · Aug 22, 2010 at 08:12 PM

Checking "Freeze Rotation" on the rigid body solved my problem.

Duck writes in this answer :

Because your cube is actually scraping along the ground (unless you've turned off gravity), it may be that at certain times during its motion, your cube is actually in contact with the ground with fewer than all 4 of its bottom points. If this occurs, the friction against the ground may asymmetrical and cause your object to move in a direction other than its forward direction.

Checking Freeze Rotation prevents the force from rotating the collider (a box collider) from changing the contact with the ground surface - at least that is my best guess to why it works.

Ofcourse, if you need the physics rotation, this will not help.

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
1

Answer by Cyb3rManiak · Aug 22, 2010 at 11:35 AM

Well, first of all - you shouldn't usually mix physics and direct transformations...

From the Unity Manual:

To control your Rigidbodies, you will primarily use scripts to add forces or torque. You do this by calling AddForce() and AddTorque() on the object's Rigidbody. Remember that you shouldn't be directly altering the object's Transform when you are using physics.

So either you go the physics way and translate and rotate it using RigidBody.AddForce() and RigidBody.AddTorque() or - rotate using Transform.Rotate(), and move using Transform.Translate(). Just try and not mix and match, since the results won't be predictable.

Also check out the meaning of a kinematic RigidBody in the docs. Maybe it will let you understand the whole thing better.

If you don't need physics (And trust me when I say - most times you don't), try looking at the different character controllers on both the wiki and the standard assets to see how to play around with acceleration, relative vectors and "fake" physics using only the Transform component. I usually avoid using physics for controllers unless it's absolutely necessary or right for the specific project.

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 Dan 6 · Aug 22, 2010 at 08:03 PM 0
Share

Thanks for your input. I agree with you in using physics only when absolutely neccessary. In this case however, i need reactions to collisions, gravity and different friction materials, so using physics in this case seems ok. I actually found a way to make it work with Transform.Rotate - see answer below.

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

No one has followed this question yet.

Related Questions

Problem with rigidbody rotation 2 Answers

How to make a RigidBody not go into ground when tilted foward? 2 Answers

How to rotate gameobject and rigidbody? 2 Answers

Lookat Direction by AddTorque? 0 Answers

Rotating a MeshCollider 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