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
1
Question by Bizio · Nov 08, 2018 at 09:24 AM · movementrigidbodytransformcoroutine

rigidbody.position not working inside coroutine

Basically as the title says, it's not working. I want to make a slide/dash movement for a player character and I'm handling it in coroutine. Earlier I was changing the position with transform.position and it was working fine but it doesn't detect collisions and I was able to dash into walls so I read that moving an object with rigidbody.position works the same but takes into account collision but after I changed it, it's not working, my character stays in one place, playing dash animation, not moving at all. Any solutions? Here's the code:

     IEnumerator Movement_Dash_Cor(float _duration, Vector3 _direction)
     {
         StartMoveAction();
 
         Vector3 startPos = transform.position;
         Vector3 targetPos = startPos + _direction * 2f;
         Vector3 movePos = targetPos - startPos;
 
         float time = 0;
 
         while (true)
         {
             time += Time.deltaTime;
             float step = time / _duration;
 
             if (time > _duration)
                 break;
 
             rb.position = startPos + movePos * slideCurve.Evaluate(step);

             yield return null;
         }
 
         EndMoveAction();
     }
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 Hellium · Nov 08, 2018 at 09:30 AM 0
Share

You should use Rigidbody.$$anonymous$$ovePosition ins$$anonymous$$d of directly assigning the value to position.

You should also use yield return new WaitForEndOfFixedUpdate ins$$anonymous$$d of yield return null ;

Finally, make sure your positions are correct (and the duration is greater than 0) and that the coroutine is correctly running (add a Debug.Log in the loop)

  IEnumerator $$anonymous$$ovement_Dash_Cor(float _duration, Vector3 _direction)
  {
      Start$$anonymous$$oveAction();
 
      Vector3 startPos = transform.position;
      Vector3 targetPos = startPos + _direction * 2f;
      Vector3 movePos = targetPos - startPos;
      WaitForFixedUpdate wait = new WaitForFixedUpdate();

      for( float time = 0 ; time < duration ; time += Time.deltaTime )
      {
          float step = time / _duration;
 
          rb.$$anonymous$$ovePosition( startPos + movePos * slideCurve.Evaluate(step) ) ;
          yield return wait ;
      }
      rb.$$anonymous$$ovePosition( startPos + movePos * slideCurve.Evaluate(1) ) ;
 
      End$$anonymous$$oveAction();
  }
avatar image xainulabdeen · Nov 08, 2018 at 09:31 AM 0
Share

Hello ! At first Debug your startPos and $$anonymous$$ovPos and check its values .

avatar image Bizio · Nov 08, 2018 at 02:53 PM 0
Share

ok it just randomly started working even if I don't return WaitForFixedUpdate, still gonna use it cause movement looks more fluid, thanks anyways.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hectorux · Nov 08, 2018 at 03:05 PM

not use position, use MovePosition(Vector3 destiny) instead

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
0

Answer by IgorAherne · Mar 08, 2019 at 01:57 PM

Also check that rigidbody's "freeze position" is not ticked (in Inspector settings).

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

187 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

Related Questions

Moving a kinematic object a fixed distance over a set time with physics in mind 2 Answers

Keep moving when window is minimized 1 Answer

Clamping rigidbody's Z position causing issues in the editor, when transform hits zero on Z axis 0 Answers

Moving multiple transforms from an array in a single script 0 Answers

[SOLVED] Object keeps sliding? 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