- Home /
How to make platform detonate when player collides?
I tried looking through previous questions to avoid repeates but I haven't found one yet... If there was then i apologize and would appreciate it if I was redirected there.
But here is my question. I'm trying to make the platform explode/detonate when the player, it's a First Person Controller,collides with it. In other words, when the player jumps on top of the platform, the platform will detonate.
There will be multiple platforms but i'm trying to get one working as of now. I have made a separate empty object called PlatformExplode and a script "explode" associated with the empty object with this in it
var player : GameObject;
var enemy : GameObject;
function Update () {
if(player.collider == enemy.collider)
{
Instantiate (explosion, transform.position, Quaternion.identity);
}
}
Am I missing something? Nothing is happening.
Unitys Explosion Framework should have all the pretty cool explosion stuff you will need. Did you already have a look at it...
Answer by C-Blunt · Jul 12, 2012 at 08:04 PM
Apologies for the C# but this is a script from a game im working on at the moment. It works without any issues.
if(other.gameObject.tag == "enemy"); //make sure you tag your enemies as "enemy" in unity.
{
SpawnExplosion();
}
void SpawnExplosion(){
Instantiate(explosionPrefab, transform.position, Quaternion.identity);
}
}
Ensure both player and enemy have colliders attached and attach this script to your player.
On a side note; try adding some de-bugging to your scripts to find out where the problem lies, this will let you know if it's the trigger or the instantiate that isn't working.
Be sure to tick the box that says "is trigger" in the unity inspector on your enemy's collider.
$$anonymous$$ay I ask why a void function is needed? void SpawnExplosion? unless it's syntax related with C#. =P I also found out that I should keep the is trigger on. I kept clicking and trying out different things to see what happens.
I assume that the is trigger is pretty much used for colliders? I mean I made a separate cube to understand the Istrigger. I made the collider a tad bit bigger. If the istrigger is checked, it pretty much blocks the first person. $$anonymous$$inda like a bigger wall. But if it's not checked, the character pretty mucuhu walks through it.
Long story short. Thank you for your help.
Answer by blackmethod · Jul 12, 2012 at 08:11 PM
In javascript I would use something like.
function OnTriggerEnter(other:Collider)
{
if (other.tag == "Player)
{
// Code for detonation
}
}
Before you try it, click on your character in the inspector and on the very top menu, change the tag to "Player" so this works. I don't know how to code the detonation, so if you know how just put it in there.
Oops, I forgot the other quotation mark -.-
Should be: if (other.tag == "Player")
Also make sure to make the box a trigger. By ticking "is trigger" box as well. Thanks C-Blunt
Answer by Ingen · Jul 12, 2012 at 09:33 PM
otherwise you can not check is Trigger, to pass through, and use OnCollisionEnter
here some reference
http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html
http://docs.unity3d.com/Documentation/ScriptReference/Collider.html
Your answer
