- Home /
two-dimensional area overlay / collision
Green short cube is moving in front of player character just above the ground - it can be rotated when holding 'q' and 'e' buttons. After mouse button 0 goes up a tall cuboid prefab is being instantiated just above the ground, with it's rotation and position equal to those of the green cube.
Green cube is just a game object with mesh - it doesn't collide with anything and has it's y position dependent on terrain height. I want to make variable private bool areaOverlay = true; whenever green cube collides with other objects such as trees, prefabs instantiated with mouse button 0, walls buildings etc, so I can make an if statement and disable the option to create new prefabs.
I was thinking of somehow checking if 2 dimensional area (x and z axis) made by four or more points is in the same space as area of other objects with certain tag/layer/script.
What's the easiest way to do it?
Answer by Rafes · Jul 18, 2011 at 08:01 PM
Colliders are probably the easiest because you can't do this calculation based on the center of an object or raycast point. You need to know if the edge of the cube is touching anything. That is, by definition a collision. You could do a raycast algorithm, but then you would still need colliders.
The only other way i can think of is to use a grid-bases system and update a 2D array so it knows where things are in the game world, like that tree, then test against it as the green cube moves. You'd probably want some sort of world scan at game start to populate the 2D array. This would probably be the way to go if you can't add colliers to everything. You would need the grid to match the cube and all objects in the world would have to obey the grid when being placed (though you can make the grid smaller, like 4 squares for the size of the cube - basically adding resolution)
would you kindly tell me how to create such colliders? I tried it using some kind of tutorial but failed. Also will the green cube be still able to penetrate through those objects?
Just add a collider component and set trigger ON. This will make it ignore physics but will still detect collisions.
A Google search yielded the following reading material. If you still have questions, please ask them!:
http://unity3d.com/support/documentation/Components/comp-DynamicsGroup.html
http://unity3d.com/support/documentation/ScriptReference/Collider.html
http://technology.blurst.com/unity-physics-trigger-collider-examples/
http://active.tutsplus.com/tutorials/unity/getting-started-with-unity-colliders-unityscript/
http://col000r.blogspot.com/2009/06/unity-triggers-and-colliders.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612150319im_/https://answers.unity.com/themes/thub/images/avi.jpg)