- Home /
Which Method is Optimal/Fastest - Raycast Vs Collider
Hello,
I have a grid of 100 x 100 empty gameObjects that I spawn on my scene, each empty has a collider attached to it, with a size of 1 x 1. So when the grid is laid out - they all fit snug against one another.
The grid is used to show where the user can, and can't deploy a building (in the future, the grid will have a white texture to show the user can deploy here, and a red to show this cell is unsuitable for deploying).
I'm currently using a box collider to on my building, that when collides with my grid colliders - sends a 'IsHighlighted' message. Which returns info about the cell - if its a good cell, or a bad one.
My question is -
I'm I better using a Box Collider, as I am doing - and using OnTriggerEnter to get all the cells under the building - which could be in the 100s count.
Or
As all my buildings are square, or rectangle - I could use a raycast on each of the corners of the building - that fires down to retrieve the cell. So my raycast will pull 4 cells in total (each corner of the building), and then use a for loop to find the cells within these four points, based on their Vector3.
Which way is better? In terms for performance and optimisation?
Answer by DawdleDev · Sep 03, 2018 at 08:20 PM
For you guys visiting this page in the future, RayCasting is WAY more performant than unity's physics system, but these guys are right in saying that this user needs to look for a better, more performant way to do things. Physics really should be used as a last resort (unless they are used as actual physics, but even then it might be better to modify the original system than use what they have).
Answer by korbul · May 15, 2014 at 09:18 AM
Having hundreds of collides is not the way to go for solving a problem like that. I recommend you hold a matrix in memory of the size you need (ex: placement[100][100]). This matrix will hold 1 if that position can have building placed on and 0 otherwise (or vice versa).
Now when you want to place a building you get it's position and check that with the placement matrix. So lets say you want to place a building at x 45, y 56. If placement[45][56] == 1, place building else, don't place building
having a big matrix like that can be very daunting to fill manually, so I recommend you make or use some kind of editor.
Using the technique above should yield greater performance that your original idea.
Okay - that makes sense if my building was 1 x 1. But what happens if my building is 5 x 10. It will only check the 'placement' for the centre axis of my building object, which will result in a return of one placement. Eg: Building wants to be placed at (20,50). Building is 3 x 3 - thus my centre axis is at (2,2) - how would I check the 'neighbouring' cells within the 'placement' that my building is overlapping?
Well you already provided a solution for this :) You mentioned that you can get the position of the building's corners.
Lets take this example your building is 3 x 5. You want to place it at x: 10 y: 10. The center of the building will be at 10 10. but the corners will fall at {(9,8),(11,12)} (you only need to points to describe a rectangle). now we only have to check the placement matrix in between those 2 positions.
{(9,8),(10,8),(11,8),(9,9), .... ,(10,12),(11,12)}
if any of those positions is 0(not buildable zone) in the matrix, then you can't place the building
Answer by Johanny · May 15, 2014 at 12:56 PM
Why not put a collider only on placed buildings? Thus you can check for collision with the building you try to place. Then you just have to check for collision between placed buildings and the building you try to place. If it collides, you display it as "not placeable here" and in the other case, you build it. Could it solves your problem?
But what if you want certain areas of the map to be unbuildable?
Your answer
![](https://koobas.hobune.stream/wayback/20220613145102im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rect from two points issue 0 Answers
How to use a Raycast to see the distance traveled within a collider. 2 Answers
Move player to specific location and avoid obstacle. 2 Answers
Im trying to create a word game with a twist 0 Answers
Raycast not detecting ANY HITS AT ALL when starting inside a collider. 0 Answers