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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Better alternative to raycast collisions in a 2D platformer?

Discussion in 'Physics' started by corlenbelspar, Jun 9, 2015.

  1. corlenbelspar

    corlenbelspar

    Joined:
    May 1, 2015
    Posts:
    14
    Like the title says, is there a better way to do collision checking in a 2D platformer other than raycasts? I have a raycast collider detection system set up right now but when I have more than 100 entities on the screen using it, things start getting lagged reeeal fast.
     
  2. corlenbelspar

    corlenbelspar

    Joined:
    May 1, 2015
    Posts:
    14
    The main problem I'm having is I used this platformer tutorial that works off of raycasts for collision detection. It has to do the raycasts in fixedupdate to check for obstacles every time a platformer entity moves and so their movement will be framerate independent. But when I have 100 entities on screen using the raycasting, it results in 21,000 raycasts per frame which is outright ridiculous I think.
     
  3. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    I have never used 2d in unity, but are you not able to just use a 2d rigidbody and a 2d collider?

    So you are doing 210 raycasts per object? What about the 2d boxCast or circle cast? Can you use those instead to try and reduce the amount of casts you need to do?
     
  4. corlenbelspar

    corlenbelspar

    Joined:
    May 1, 2015
    Posts:
    14
    I guess it's 210 raycasts per object when you do it every fixedupdate with 3 raycasts horizontally and 2 vertically. Can you tell me how to do boxcast? That sounds like it might be better than what I'm doing.
     
  5. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
  6. corlenbelspar

    corlenbelspar

    Joined:
    May 1, 2015
    Posts:
    14
    Thanks HiddenMonk. I'll try it out and let you guys know the results for people who stumble on to this looking for an answer.
     
  7. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    If you are only doing 5 raycasts per object, I dont think the boxcast would be much of a help. If your raycasts dont have a distance set to them, maybe try giving them only distances they would need.
    Also, are you sure its the raycasts? Make sure your draw calls are not going up when you have all your objects in the scene. That could be a major frame killer. To see your draw calls go to the stats button on the game tab when you hit play. If it is your draw calls than you will need to look into dynamic batching.
    Also make sure your other objects dont have cameras attached to them or something, as I think that might increase the draw calls (or even double it).
     
  8. corlenbelspar

    corlenbelspar

    Joined:
    May 1, 2015
    Posts:
    14
    Well I did a rough version of boxcasters and it actually performed more poorly than with just raycasts. I will look into dynamic batching at any rate.
     
  9. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    That is what I figured. I thought you were doing 200+ raycasts per object, but then you said only 5.
     
  10. corlenbelspar

    corlenbelspar

    Joined:
    May 1, 2015
    Posts:
    14
    It's probably because fixedupdate makes it jump up to 200+. I'm going to experiment a bit with it some more.