Search Unity

Raycast collision, delta time, and you!

Discussion in 'Scripting' started by PicklesIIDX, Nov 9, 2010.

  1. PicklesIIDX

    PicklesIIDX

    Joined:
    Aug 4, 2009
    Posts:
    32
    Hey there,

    So I'm using raycasting to make custom 2d collisions with 3d objects (yay platformer). I'm curious if there is a good way of doing this while remaining framerate independent. For example, if my rays have a distance of 0.5, normally they will work fine when the framerate is stable (if movement is based off of deltaTime). But, if the framerate skips, the movement increases, which allows the objects to breach that 0.5 ray distance, and do things like go through walls.

    I'm thinking that I may also have to multiply the rays by deltaTime...but it doesn't seem that simple...The object needs to know it has physically moved past the point where the ray wouldn't check collision...

    Currently I have simply removed the deltaTime, and am no longer framerate independent, but I know that my objects won't go through walls.

    Any suggestions on a way to use raycasting intelligently so that I can stay framerate independent?

    Thanks.
     
  2. BlobVanDam

    BlobVanDam

    Joined:
    Jun 28, 2010
    Posts:
    88
    Your raycast length should be the distance your object is moving for that frame, and do the check before you move the object. My game uses only raycasting to move my player (a 2D platformer), and it's pretty good even down to about 10fps (I haven't done a lot of testing at lower framerates yet).
     
  3. PicklesIIDX

    PicklesIIDX

    Joined:
    Aug 4, 2009
    Posts:
    32
    That does seem to work. Thanks there.

    Although I do want to tighten it up a bit. If I let it figure out the last bit of distance itself before a collision, it will slide the character all over the place. A step past that was to just have the character stop on any collision detected. Because of the nature of that, we sometimes get gaps between the player and the wall...

    I'm going to play around with it, but any ideas on how to make that character always perfectly flush with the colliding surface?