- Home /
Help With Colliders
Hello,
I have a prefab of a turret (which has its own collider - trigger), I also have a game boundary which is just a trigger collider (tagged GameBoundary).
I basically want a boolean to be true if turret touches game boundary.
EG:
if(turretCollider collides with collider tagged GameBoundary){
madeUpBoolean = true;
}
So if the turret makes contact with the game boundary: madeUpBoolean = true
How would one do that?
Thanks
Answer by Justin Warner · Mar 31, 2011 at 04:42 PM
You'd use the OnCollisionEnter, and then take what is colliding with it, and check the tag of that object, and if it's equal, than do it:
function OnCollisionEnter(collision : Collision) {
if(collision.gameObject.tag == "GameBoundary")
{
madeUpBoolean = true;
}
}
but the function OnCollision has to be on an object that is getting collided right? I have a script that isnt attached to either of the objects. So I want to get the info from the turretCollider, and the gameBoundaryCollider
Not following... If you do this, you can access whatever variables (Assu$$anonymous$$g madeUpBoolean) from another script with.... I don't know, I always used broadcast message which kind of sucks, I think GetComponent and you can get a script of an object... And then use it accordingly. So then put the script on the turret, and on the main script, just access it...? Would that work for you?
Answer by e-bonneville · Mar 31, 2011 at 04:52 PM
Put this on your turret:
JS:
function OnCollisionEnter(col : Collision) {
if(col.collider.GameObject.CompareTag("GameBoundary")) {
madeUpBoolean = true;
}
}
C#:
void OnCollisionEnter(Collision col) {
if(col.collider.GameObject.CompareTag("GameBoundary")) {
madeUpBoolean = true;
}
}
Please note that the above code is untested and may contain errors.
Your answer
Follow this Question
Related Questions
What's wrong with OnCollisionEnter? 2 Answers
OnCollision Script... 2 Answers
A limit to the number of tags? 2 Answers
My script says object is not colliding with the tag! But it is! 1 Answer
Collision Issue 1 Answer