- Home /
Altering Collision Based on Proximity
I am currently creating a randomly generated road in Unity. The road itself works exactly as intended, but now the collision becomes the tricky part.
My road is made of square tiles, and I want the player to walk on the interior. What I have done is I set up for separate Box Collider 2D to the prefab which create the cubes in this road.
What I want to do is have each of the tiles check for if there's a tile to any given side of, and if there is, then disable the respective Box Collider 2D based on that. If there is another tile to the direct left and to the direct right of one tile, it will disable its Box Colliders that lie on the left and right sides. If one detects a tile above and to the right of itself, it will disable the top Box Collider 2d as well as the right, and so on.
Th two major questions here are:
How can I do a check of if another object is within proximity (in this case basically touching it) within the script?
How can I individually turn off Box Collider components on a prefab, rather than the whole lot of them?
Thanks a bunch!
Answer by Anxo · Oct 24, 2014 at 06:46 PM
Look at the docs for Raycast and Raycasthit to draw a ray somewhere to see if it hits a collider, that will show you if there is something in the proximity, you could also check it with sphearCast.
To turn off a component of an object you can collect it in a varible first or just find and turn it off like so.
player.gameObject.GetComponent().enabled = false;
Had a feeling it would have to be raycasting. Will check on that.
Is there a way to distinguish between components though? That script won't discern between which component it will disable. I need to be able to specifically target the cardinal directions to enable/disable them accordingly. They all have different values, of course, but how can I differentiate them in code?
what do you mean? the GetComponent is specific. Unless you mean that you have multiple copies of the same type on one game object, which I can not find the sense in but should it be the case that you are using instances of classes like variables then you could add a variable to the component that is unique to each component and check it.
SomeComponent wantedComponent = null;
SomeComponent componentsOfTypeOnObject = gameObject.GetComponents<SomeComponent>();
foreach(SomeComponent sc in componentsOfTypeOnObject){
if(sc.assignedID == "notGoodPratice")
{
wantedComponent = sc;
}
}
WUCC
But really, I would not have multiple instances of a single type component on one game Object. There are times of-cause where it is needed but I am doubtful that this is the case. How are you using it, why do you need to ad multiple copies of a script to a gameobject.
What would you reccommend other than having the four box colliders? At any given time the box (in this case) will be blocking two sides of the path whilst allowing the other two to go by. That's why I needed the Raycast solution, to detect which sides should be opened up.
I wanted to try and do this in a reusable as possible way, and especially for the corner pieces, not have to make a separate object to be spawned at the four ends of the square.
I usually make child objects who each have their own collider, specially if I need to differentiate between them at collision.
Your answer
Follow this Question
Related Questions
how do I enable collision? 0 Answers
Tilemap Collider 2D preventing objects from moving 2 Answers
How do you trigger a collider to become enabled after the player collider isn't touching it anymore? 4 Answers
2DColliders going through eachother 1 Answer
How do I get onmouseenter to trigger even if another collider is blocking th? 0 Answers