- Home /
 
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();
     }
 
              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();
  }
 
                 Hello ! At first Debug your startPos and $$anonymous$$ovPos and check its values .
ok it just randomly started working even if I don't return WaitForFixedUpdate, still gonna use it cause movement looks more fluid, thanks anyways.
Answer by hectorux · Nov 08, 2018 at 03:05 PM
not use position, use MovePosition(Vector3 destiny) instead
Answer by IgorAherne · Mar 08, 2019 at 01:57 PM
Also check that rigidbody's "freeze position" is not ticked (in Inspector settings).
Your answer
 
             Follow this Question
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