Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

[Unity Question] How are you all handling collision detection?

A topic by Andrei Tache created Sep 30, 2019 Views: 230 Replies: 3
Viewing posts 1 to 3
Submitted

Hi everyone! I've got more of a Unity specific question than a jam one, but since I wasn't able to find an answer on the web, I thought I'd ask here :)

I'm not sure how to detect collisions between objects that are not children of the script object. (I've gone the route of having one singular master instance with the script attached to it, instead of having the script attached to everything.)

As I've said, I've searched for "detect trigger enter from another object", but people are basically saying it's dumb to do (go figure ;D ) and suggesting creating a listener script (which goes against the rules of the jam..)

I have managed to get something working using Unity's function Physics2D.BoxCastAll(), however, I am running it for what I plan to be maybe hundreds of objects (of rather small sizes, but still) and in Update, so it is having a massive impact on framerate...  I know I can make it update less often (ex only 5x a second, rather than 60x a second) and that will undoubtedly help, but I wanted to know if maybe there's a better way of doing it.


So, back to the title of this post, how are all of you handling collision detection?

(Thanks for the help, I'm really enjoying the challenge of this jam so far :))

(1 edit)

I am trying really hard to avoid that and doing collision by hand comparing Vector distances,  but i think this wont take me far, i literally sort all boxes by distance and take the fist one,  

var boxesCandidates = boxes.Where(r =>
{
    if (r.id == _hoverBox.id) return false;
    var relativeY = r.startPosition.y - _hoverBox.startPosition.y;
    return verticalAxis > 0 ? relativeY > 0 : relativeY < 0;
});
boxesCandidates = SortByDistances(boxesCandidates, false);
var candidate = boxesCandidates.First();
SetHoverBox(candidate);

PD: Sorry for not been really helpful

Submitted

Sorry, you've lost me at the code :)

If I understand it correctly from your first sentence, are you checking the distance between Obj A and Obj B and then seeing if the distance is less than their respective widths/heights?

If so, then how would you handle rotation? Maybe it'd be easier to just not do it and pretend everything is a sphere/circle so you'd just check in a radius around it?


Anyhow, that's an interesting approach; I think I'll stick with raycasts, even if they are more resource intensive, simply because they're easier, but do let me know how it work out!

i am not handling rotation, as you say i am pretending everything is a sphere, it is working so far but yet as you say this is not readable nor easy. i should not doing it that way.