- Home /
Raycasting based collision detection fails on slow framerates
I'm experiencing an odd issue with my collision detection. I'm using the Update
method to read input, and I have the collision detection code running in the FixedUpdate
method (per the usual suggestion so that the code is always executed at a fixed rate, regardless of framerate). The fixed timestep is set at the default 0.02.
However, when the framerate is low (below 30), the collision detection doesn't always work. The player can easily fall through the collision layer, though sometimes they don't.
It's my understanding that FPS shouldn't have an effect since I'm calling the collision detection logic in FixedUpdate
.
If you'd like to see the actual collision detection code, here's a Gist to an abbreviated version.
Edit: Based on Gruffy's comment, I rearranged some of the code so that all of the input polling occurs in Update
and I perform the actual raycasting and final translation in LateUpdate
, which alleviates the problem, but doesn't completely remove it.
If you were updating your position in Update, then any collision detection in Fixed Update would be too late for the moment of collision as Update is called before Fixed Update regardless, so anything in fixed update that relied on a translation in Update could never receive, churn and output the data in time.
consider this... Fixed update loops every actual second Update loops at 60 frames per sec with the above in $$anonymous$$d, your dilemma is pretty clear, as my previous waffle up-top digresses upon
Who told you Collisions go into FixedUpdate anyway. Physics is happy in there but Unity offers theri own functions to carry out collision outside of any FixedUpdate method call.
Collision is found through one of two Unity methods(to start with), void/function OnCollisionEnter(Collision col/ col: Collision) or void/function OnTriggerEnter(Collider col/ col: Collider) (C#/UnityScript)
Humbly, I think its best you read this before you go any further bud.
Collision Unity Scripting Reference
Hope it help-s for now.... Why dont you post your code, it may be easier to example (A) what im saying & (B) what it is exactly in the code that is causing you your problem Take care dude:0 Gruffy
I've managed to cap the framerate on a Windows build through the use of Application.targetFrameRate
, and when I set the framerate to 20, I'm experiencing the same issue I see on Android, so the problem is related to a slow framerate, which I feared.
But you may be right about when I'm applying the translation. First, I tried updating my position in FixedUpdate
as well (see the code sample). I've even moved the input polling inside FixedUpdate
, just to see if that had any effect. That didn't help. I'm now doing all of the input polling inside Update
and using LateUpdate
to perform the actual raycasting and perform the final translation, which seems to alleviate the problem greatly, but occasionally I'll notice the player go through the collision layer when he shouldn't.
Since I'm doing a 2D platformer, I'm using raycasting to detect collision layers and adjust entity movement as appropriate, so I can't use the Unity OnCollision methods.
I've included a link to the code in the original question, but here it is again, https://gist.github.com/$$anonymous$$adballNeek/9162e6f05b88e49b0a33.
Any progress with this? I've also been working on a platformer system that uses raycasting to detect when it hits the ground, etc. Sometimes (very rarely) when I have a sudden drop in frame rate, my character will go right through the ground.
Any help would be appreciated.
Okay, I will take a further look at it. Gimme the day to come back to you , as I is WELL busy today
@ShaneTheVeryTall The code in the gist I linked has been working fine for me now, I haven't noticed the issue anymore. You may want to open up a new question with your code for further help.
@Gruffy I'm not sure what you're referring to. As already commented, the gist I linked most certainly isn't a single script file (it's abbreviated code for three separate classes that I've pasted into a single gist, for the sake of brevity), nor is it slow. The framerate drop I was witnessing was due to a completely separate issue, which I've since resolved. Thanks again for the help though.
Answer by Xtro · Jan 28, 2014 at 07:59 PM
Do the raycasting and the input reading both in the FixedUpdate.
Why do you even read the input in the Update if you are gonna move it in FixedUpdate?
Can you please mark the correct answer if you fixed that problem?