Search Unity

How do I get an object to move to a position without getting it to bounce in place?

Discussion in '2D' started by JohnHoward312, Feb 17, 2021.

  1. JohnHoward312

    JohnHoward312

    Joined:
    Nov 12, 2018
    Posts:
    21
    When I call move position on an object, it moves it self to a spot. However, sometimes it will just bounce back and forth in place when it reaches an area close to the position but not quite there yet. How do I stop this?
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Can you post your code?
     
  3. JohnHoward312

    JohnHoward312

    Joined:
    Nov 12, 2018
    Posts:
    21
    Vector3 direction = (startPos - (Vector2)transform.position).normalized;
    rb.MovePosition(transform.position + direction * 2f * Time.deltaTime);
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    If it "bounce back" then it must be a Dynamic body-type which you use when you want it to respond to collisions/gravity (etc) so if you don't want it to responsd to collisions then use a Kinematic body-type which is really what "MovePosition" is for.

    If you're not using a Dynamic body-type then I would suggest providing more information on your posts rather than waiting for questions. Post code, detail of the body settings, things it's colliding with etc. Basically something beyond the symptoms.
     
    JohnHoward312 likes this.
  5. JohnHoward312

    JohnHoward312

    Joined:
    Nov 12, 2018
    Posts:
    21
    I know what's happening but I don't know a quick way to fix it. When I call move position, the object will move x units towards the startPos point. But I think maybe movePosition causes the object to overshoot, so it goes backwards the next frame and so on in a "bouncing" sort of way. Is this how movePosition is supposed to function or is it supposed to correct for the overshoot?
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    If you know what's happening then you must know this is incorrect. MovePosition does that, moves to the position you specify; there's no overshoot otherwise that'd be "MovePositionAndThenABitMore" which it isn't. ;)

    You didn't confirm if it's a Dynamic body so I'll assume it is and you're not performing any queries to determine where to move to etc.

    If you're using Discrete col-det then that means bodies move to their next simulation position then the next simulation steps will move it out of overlap. If you use Continuous then it won't move into overlap but it's more expensive. This, of course, has nothing whatsoever to do with MovePosition but simply moving.

    Rigidbody2D.collisionDetectionMode
     
    JohnHoward312 likes this.
  7. JohnHoward312

    JohnHoward312

    Joined:
    Nov 12, 2018
    Posts:
    21
    Ok here's some information:
    upload_2021-2-20_18-52-38.png
    I also have it on a distance joint with a rigidbody.
    upload_2021-2-20_18-53-6.png
    Maybe this is affecting something?
     
  8. JohnHoward312

    JohnHoward312

    Joined:
    Nov 12, 2018
    Posts:
    21
    I turned off distance joint but it is still bouncing. It kind of gets really close and then just bounces around:
    upload_2021-2-20_18-54-37.png
     
  9. JohnHoward312

    JohnHoward312

    Joined:
    Nov 12, 2018
    Posts:
    21
    I also put it on continuous but that isn't seeming to help either.
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Without seeing what you mean it's impossible to tell you what the issue is. Even then, it's hard.