Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Problem with DontGoThroughThings

Discussion in 'Scripting' started by Longman1981, Mar 19, 2018.

  1. Longman1981

    Longman1981

    Joined:
    Feb 16, 2018
    Posts:
    16
    There's a famous script DontGoThroughThings
    which fixes problems with collision for fast moving objects. Basically what it does is to check previous physics frame position, than cast ray from that position to current one and see if there's collision inbetween, If there is - than we change position to match that collision.
    Now problem that I see here is that before you modify position manually in fixedupdate, update might have already run and player which passed collision point might have already been drawn on screen, so eventually you might see that player goes through object and than moves backward. Do I miss something? Or is that actually how DontGoThroughThings work?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    Never used the script (or heard of it). But looking at it, it only has a fixedUpdate, and looks like it cast a ray, detects collision and moves based on that. So it should never draw ahead of any collision.
     
  3. Longman1981

    Longman1981

    Joined:
    Feb 16, 2018
    Posts:
    16
    It casts array from previous position to current position, frame rate isn't tied to fixed update so by the time it casts a ray it seems like it might already be drawn to scene. I'm not sure why do you think that it should never draw
     
  4. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    The situation that you're describing would only happen if you're moving an object in Update, and then expecting the physics system to catch any errors. This is not how things are supposed to happen. Mixing motion in the two update loops is best avoided.

    If you're moving the player with physics, then all translation should be done inside of the FixedUpdate loop. If you're moving the player with code (in Update) then this script should be modified to do its work in Update.
     
  5. Longman1981

    Longman1981

    Joined:
    Feb 16, 2018
    Posts:
    16
    if you modify your player in fixedupdate doesn't it get drawn every frame update anyway? you move player 1 unit right in fixed update, start from (0;0). we have wall at (1.5;0). First fixed update: there's no previous position set so just move to (1;0). now frame is updated and player is drawn at (1;0). Second fixed update: previous position is (0;0) we draw ray from (0;0) to (1;0) there's no obstacle so move right once more, now our position is (2;0), now before next fixed update is run if frame is updated we're already at (2;0)
     
  6. Longman1981

    Longman1981

    Joined:
    Feb 16, 2018
    Posts:
    16
    Hmm I haven't noticed that it's written rigidbody.position rather than transform.position, I'm not aware of differences between those, maybe that's the reason