• 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
1
Question by TheMPC · Apr 20, 2010 at 06:43 PM · physicsaddrelativeforce

AddRelativeForce miscalculations

Good day kind people,

Let me start with explaing my intentions:

It is my intention to create 1 cube that can be moved around a level by pressing the WASD keys, this gave me no problem and you could easely move the cube around the level.

This however was done by using the Transform.position, but i wanted a cube that had physics, so i changed the Cube to have a Cube collision and a Rigidbody,

Now in stead of using the Transform.position (which wouldn't give me my physics) i used the AddRelativeForce (which would add force to the physics object forward into the direction it is rotated to, correct me if im wrong please)

Done so using the following code:

float v = Input.GetAxis ("Vertical"); float h = Input.GetAxis ("Horizontal"); Vector3 relForce = new Vector3(h, 0, v);

mybody.AddRelativeForce(relForce * 20);

Now here is my problem:

The AddRelativeFoce function isnt doing what i think it should be doing, the 2 things that i noticed:

1: It isnt acurate in checking the rotation and adding force to my object in that matter, for instance my cube has a rotation of 36 degree and its pushing my cube "forward" like it only has a rotation of 30 degree.

2: If we ignore my last matter and add some relative force to my Cube regardless of its rotation i noticed that if i were to release my key to go forward it slows down, now that ofcourse is supposed to happen but: instead of moving forward and slowing down, it moves forwards and slows down with a little offset to the left or right (yes it makes a little curve sideways).

If my explanations aren't clear enough or you would like some visual material to see, just drop a comment and ill create a video of my problem occuring.

Greetingz, TheMPC

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 spinaljack · Apr 20, 2010 at 07:37 PM

AddRelativeForce is relative to the object's rotation, AddForce is on the world axis.

If you use Get Axis then it'll be analogue movement (you can print the values or display them on the inspector to see) depending on how sensitive you've got the input manager set to. Try using GetKey instead to get digital input.

The drag values of the rigid body dictates how it'll behave like air resistance, try setting it to 0 for perfect straight line travel, it will keep moving forever though. Also gravity might be pulling it down.

Another thing which may be causing the drift might be that the objects centre of gravity might have moved if the objects is an odd shape or has moving children.

Comment
Add comment · Show 5 · 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 TheMPC · Apr 20, 2010 at 07:39 PM 0
Share

Unfortunatly, no i do not ant AddFoce, i WANT Force reltative to the objects rotation, only its not 100% acurate

avatar image spinaljack · Apr 20, 2010 at 07:42 PM 0
Share

Ah right, the wording was confusing

avatar image TheMPC · Apr 20, 2010 at 07:53 PM 0
Share

no problem and sorry for it, i thank you for you help ofcourse and will try your new sollution right away.

avatar image TheMPC · Apr 20, 2010 at 08:00 PM 0
Share

Thhank you for the perfect answer ^^ i remove my gravity and it was flawwless, im still wondering how to make this happen with gravity, but ill try that tomorrow :)

avatar image duck ♦♦ · Apr 20, 2010 at 08:12 PM 0
Share

See my answer for a solution which will work with gravity enabled.

avatar image
2
Best Answer

Answer by duck · Apr 20, 2010 at 08:08 PM

AddRelativeForce does work in the manner that you describe.

What may be causing the small amounts of erratic movement are the cube's contact points with the ground.

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.

If you set both the cube's and the ground plane's colliders to use the preset "Ice" physic material, there will be no friction between the objects, and you should find your object moves exactly in the directions that you expect (although it will continue to slide across the surface when you let go of the keys!).

To solve this, you can then turn up the "drag" value on the cube's rigidbody to simulate the friction it would normally have with floor.

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 TheMPC · Apr 20, 2010 at 08:18 PM 0
Share

Superb, this solves my problem permanent :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

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

Physics: Is it possible to count the number of colliders hit with OverlapSphere? 1 Answer

Make car CenterOfMass change dynamically 1 Answer

If statment to check if player is within collider 1 Answer

Parenting player to car on click. 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