Search Unity

From Mathf.MoveTowards to rigidbody.MovePosition

Discussion in 'Scripting' started by herbie, Aug 24, 2015.

  1. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Hi Everybody,

    For a couple of days now I'm trying to "convert"
    Code (JavaScript):
    1. transform.position.x = Mathf.MoveTowards(transform.position.x, target.transform.position.x, Time.deltaTime*speed);
    into something with rigidbody.MovePosition:
    Code (JavaScript):
    1. rigidbody.MovePosition(transform.position + target.transform.position * Time.deltaTime*speed);
    but I do not succeed.
    The only thing I want is that the gameobject goes towards the targetobject in the x-direction using rigidbody.MovePosition.
    Can somebody point me in the right direction?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Rigidbody.MovePosition doesn't quite work like MoveTowards, but you could just combine them:

    Code (csharp):
    1.  
    2. Vector3 movePosition = transform.position;
    3.  
    4. movePosition.x = Mathf.MoveTowards(transform.position.x, target.transform.position.x, speed * Time.deltaTime);
    5.  
    6. rigidBody.MovePosition(movePosition);
    7.  
     
    sean244 and CastleIsGreat like this.
  3. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Thanks for reply.
    The problem is that you have some kind of jitter when the character is walking against a wall using Mathf.MoveTowards.
    With rigidbody.MovePosition you don't have that jitter I think.
    When you combine Rigidbody.MovePosition and MoveTowards, you still have that jitter.
     
  4. Rostam24

    Rostam24

    Joined:
    Mar 5, 2014
    Posts:
    119
    Herbie, looks like you should be doing:
    ((transform.position+ target.transform.position) * Time.deltaTime*speed);
    Instead of:
    (transform.position+ target.transform.position* Time.deltaTime*speed);

    I hope this stops the jittering that you mentioned, it's really annoying :)

    edit: In fact, you might want to use rigidbody position instead of transform position for this...
     
  5. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    If you're doing this inside of Update instead of FixedUpdate, that might also be a cause of the jitter.
     
    sean244 likes this.
  6. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Thanks.

    It's still not working good.
    With rigidbody.MovePosition the gameobject goes towards the targetobject but it doesn't stop when it is there (of course).
    Is there another way to move a rigidbody from one point to another with the physics engine, just like Mathf.MoveTowards?
     
  7. ExtraAmmo

    ExtraAmmo

    Joined:
    Mar 12, 2015
    Posts:
    11
    Check this out:

    http://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html

    So, if you are in FixedUpdate() you will want to use MovePosition WITH MoveTowards to get smooth motion over time. MovePosition only smooths between fixed updates since multiple render updates occur between fixed updates.

    However, directly moving a rigidbody against a collider (or wall) will always cause a jitter. If you want the rigid body to respect physical collisions but be moved through script, you can try moving it with forces.

    Code (csharp):
    1.  
    2. float speed = _rigidbody.velocity.magnitude;
    3. _rigidbody.AddRelativeForce(rightLeftAcceleration/ (speed + 1f), 0f, forwardBackAcceleration/ (speed + 1f), ForceMode.Acceleration);
    4.  
    Dividing by the current speed plus one makes the rigidbody approach a steady speed instead of going faster and faster.