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

3 Answers

3

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

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.

Someone can delete this.........