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 Game_Maker21 · Dec 29, 2020 at 08:28 PM · physicsrigidbodykinematicrigidbody.moveposition

[SOLVED] What is the best way to move my rigidbody car in unity?

I am currently making a car controller in unity and looking for a way to move my car while being able to correctly collide with obstacles. I also want the car to be able to drive on uneven roads. The road is kinematic while the obstacles have gravity and are not kinematic. 1 -- I tried using transform.Translate but as expected, it just ended up ignoring colliders.

         verticalInput = Input.GetAxis("Vertical");
         transform.Translate(transform.forward * speed * verticalInput * Time.fixedDeltaTime);

2 -- I then tried using Rigidbody.MovePosition() while the car has gravity and is not kinematic. The car was able to correctly drive on uneven roads but wasn't successful in pushing away the obstacles. The obstacles were pushed below the car (between the wheels).

         verticalInput = Input.GetAxis("Vertical");
         rb.MovePosition(rb.position + transform.forward * speed * verticalInput * Time.fixedDeltaTime);

3 -- I tried using Rigidbody.MovePosition() while the car is kinematic with self-coded gravity. The car fell down and ignored the road collider. But I noticed that this approach made the car interact well with the obstacles.

         verticalInput = Input.GetAxis("Vertical");
         rb.MovePosition(rb.position + transform.forward * speed * verticalInput * Time.fixedDeltaTime);

4 -- I tried altering the Rigidbody's velocity while the car is not kinematic. The car tumbled instead of moving like an actual car. The car also can't be steered.

     verticalInput = Input.GetAxis("Vertical");
     // Move the vehicle
     rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, speed * verticalInput * Time.fixedDeltaTime);

5 -- I used Rigidbody.AddForce() while the car is not kinematic. I had to set the speed to like 100000 then the car started very very slow then accelerated to a very very fast speed. (I want the car to move like a transform.Translate)

         verticalInput = Input.GetAxis("Vertical");
         rb.AddForce(transform.forward * speed * verticalInput * Time.fixedDeltaTime);

Btw, I'm using Unity 2020.1

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
1
Best Answer

Answer by Edy · Jan 02, 2021 at 04:27 PM

1 and 2 are "teleporting" the rigidbody. The physics engine tries to resolve collisions after the teleport, but then you teleport the rigidbody again. This makes collisions highly inconsistent. Sometimes some collisions work, other times some collisions don't work.

3 - In kinematic rigidbodies the position is controlled by the user, not by the physics. The rigidbody will go wherever you move it to, ignoring any collision geometry.

4 - Similar to 1, but with velocity. The physics engine computes some velocity, then you override it, and the engine computes the next step using the new value. The result is inconsistent. Additionally, velocity is a time-independent magnitude here. Time.fixedDeltaTime doesn't make sense here.

5 - AddForce is the way to go, but this implementation is incorrect. Also, again, Time.fixedDeltaTime doesn't make sense here.

One correct way is calculating the velocity you want, then applying the difference with the current velocity via AddForce with ForceMode.VelocityChange. This will cause the rigidbody to move at the expected velocity, and collisions will be resolved properly because all the work is done by the Physics engine:

 rb.AddForce(expectedVelocity - rb.velocity, ForceMode.VelocityChange);

You may also want to learn about the different combinations of rigidbodies and colliders, and proper example usages of each case:
https://forum.unity.com/threads/help-understanding-all-8-collider-rigidbody-combinations.525258/

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 Game_Maker21 · Jan 04, 2021 at 02:10 AM 0
Share

Thank you very much! :D

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

215 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

Related Questions

Child Rigid Body collision problem 2 Answers

Switch off Kinematic for a Rigidbody in response to an explosion or impact? 1 Answer

Problems with ragdolls and no gravity 0 Answers

Projectile collides, freezes, but flips to weird angle. Help! 0 Answers

Realistic collisions with a kinematic rigidbody? 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