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 /
  • Help Room /
avatar image
0
Question by javixtop · Aug 23, 2016 at 01:49 PM · movementrigidbodygravity

Issue with Rigidbody movement one step at a time and gravity

I'm trying to make a game where you can select which action you want to perform one at a time. e.g. the game waits for the user to say which action to do, then executes it and then wait for the next one, and this is done in a co routine which then starts whatever the co routine for the action is. So I based my move routine on the 2D roguelike game, this is what I have:

 public IEnumerator MoveRoutine(){

     Vector3 end = transform.position + transform.forward;

     float sqrRemainingDistance = (transform.position - end).sqrMagnitude;

     //While that distance is greater than a very small amount (Epsilon, almost zero):
     while(sqrRemainingDistance > float.Epsilon)
     {
         //Find a new position proportionally closer to the end, based on the moveTime
         Vector3 newPostion = Vector3.MoveTowards(m_RigidBody.position, end, m_Speed * Time.deltaTime);

         m_RigidBody.MovePosition (newPostion);

         //Recalculate the remaining distance after moving.
         sqrRemainingDistance = (transform.position - end).sqrMagnitude;

         //Return and loop until sqrRemainingDistance is close enough to zero to end the function
         yield return null;
     }

 }

Now the issue comes with the jumping and gravity part. Since there are "holes" in the field where the player can fall off I want to have gravity on the player Rigidbody. However, if I activate this then the movement routine gets stuck in an infinite loop, because for some reason the value of Y that the player's rigidbody has goes to like 0.750000004 instead of the original 0.75.

Any thoughts on this? What can I make different? I have been reading that the physics engine doesn't like you to move stuff with MovePosition, but these are comments saying that it should be done in FixedUpdate instead of Update, but I'm doing it in a coroutine.

Thanks in advance guys :)

Comment
Add comment · Show 3
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 Owen-Reynolds · Aug 23, 2016 at 02:21 PM 0
Share

The 0.75004 value is normal. When you let physics move you around, like you are with gravity, you won't get the exact numbers you'd think. There are various tolerance values -- like if you stack two 1x1x1 cubes, the top cube won't be exactly 1 above the bottom one.

float.epsilon is for proper math, measuring one true rounding error. Physics isn't rounding -- it's just saying that being inside another object by only 0.01 is close enough.

$$anonymous$$aybe try some tests, in a new scene of letting an object move and fall, watch the numbers, and play with settings. That should give a feel for it.

avatar image javixtop Owen-Reynolds · Aug 23, 2016 at 03:07 PM 0
Share

Thanks. You actually gave me a cool idea. So for my Epsilon comparison I created 2 new "current" and "end" vector3d but with 0 in Y, so that I'm always checking on the movement in my X and Z axis. It works, it actually finishes the loop now. However, now while the player moves if there is a hole underneath it looks like it floats until it reaches the desired position in X or Z.

Do you know by any chance if there is a way for me to "move" an object from A to B on a single axis and at the same time let gravity do it's thing? I think that summarises my original question a lot better.

avatar image TreyH Owen-Reynolds · Aug 23, 2016 at 05:37 PM 0
Share

Yeah, consider using your own epsilon. Just some negligibly small, close-enough value.

As for the move-position and coroutine thing, you can try yield return new WaitForFixedUpdate() (ins$$anonymous$$d of yield return null) to time things correctly.

(replying to owen comment to prevent the unity comment tree)

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make my "PlayerMovement" script and "Use Gravity" checkbox (rigidbody) enable when I click SPACEBAR in game? 1 Answer

After added a rigidbody in a GameObject, it moves on rotation 2 Answers

Edit Gravity for Ramps 0 Answers

The player falls down a ladder 0 Answers

Why does the player not move around planet with gravity ? 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