- Home /
Destroy on Collision?
Ok so i have my FIRST-PERSON CONTROLLER and i want certain cubes to be destroyed as soon as the controllers collides with them. So far i use the below script on the cubes but as soon as i press Play they dissapear. What's the trick here? What kind of if statement do i need? Also when i want to use OnCollisionEnter to destroy something i attach the script to the destroyer or the destroyed? Thanks!
function OnCollisionEnter(){ GameObject.Destroy(gameObject); }
Ok i used this script attached on the cube which will be destroyed but nothing happens when i collide him with the FP Controller. I created the tag and stuff.
function OnCollisionEnter(collision : Collision){ if(collision.gameObject.tag == ("Destroyer")){ Destroy(gameObject); } }
Does one of your objects have a rigid-body attached?
" Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached." From OnCollisionEnter documentation
http://www.unity3dstudent.com/2010/07/beginner-b01-basic-collision-detection/
http://www.unity3dstudent.com/2010/07/beginner-b04-destroying-objects/
Here are some links I strongly suggest to all new users :
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/
this is the YouTube link for the above as one playlist : http://www.youtube.com/watch?v=-oXYHNSmTxg&list=PL27B696FB515608D2&feature=plcp
That is good to get started. Then start with a small tutorial, this is a simple 2D space shooter : http://www.unityjumpstart.com/ProofOfConcept_1/ : click on the videos part1.mp4 part2,3,4 =]
I found another by Eric : http://wiki.unity3d.com/index.php?title=2DShooter : http://forum.unity3d.com/threads/7883-2D-shooter-tutorial
By then you should be getting the hang of things and starting to have ideas of your own. When you decide what kind of game you want to make, then look at each part you'll need. For example, if you want to make some terrain then walk around it with a character : http://cgcookie.com/unity/2011/12/05/introduction-to-character-controllers/
Basically then just search for tutorials, there are many out there, either written or on youtube.
the Unity Wiki tutorials : http://wiki.unity3d.com/index.php/Tutorials
A big list of tutorials : http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html
A very helpful 'site, all in C# : http://unitygems.com/
Helpful page with information on using Built-In Arrays and Lists (you'll need this later!) : http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?
The unity wiki link above is very handy with lots of scripts and shaders too (just check out all the links down the left, and the tabs along the top : http://wiki.unity3d.com/index.php/Scripts )
I am using unity 4 for a school project. You basically have a first person controller, and use it to collect cubes scattered around a map. I need to destroy the cube when the controller collides with them. i have never used c sharp bu
Answer by HypoXic5665 · Apr 19, 2013 at 05:52 PM
Currently you have your cubes set to destroy themselves as soon as anything collides with them. In your case I would assume they are colliding with the floor as soon as you start the game and being destroyed. If you are keeping this script on your cubes you will want to do something like:
function OnCollisionEnter(collision:Collision){
if(collision.gameObject.tag == "Player"){
Destroy(this.gameObject);
}
}
Be sure that your Player is tagged as "Player" for this example.
I've already tried this. Nothing happens and it really bugs me since this is theoretically the right solution. Do you think it doesn't work because i use a First Person Controller as my player? That's why i wrote it in Capitals above :)
From Above: Does one of your objects have a rigid-body attached?
"Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached." From OnCollisionEnter documentation
You will need to be sure that neither of the objects are ticked as "isTrigger" in the inspector under their collider properties and, as from the documentation above, be sure that at least one object has a non-kinematic rigidbody attached to it.
[2]: http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html
The First Person Controller is an exception to the rigidbody rule.
So the Unity First Person Character controller can activate trigger events without a rigidbody, but for any other object, you need a rigidbody component attached to one of the objects. A basic rule is : if it moves, it needs a rigidbody !
And from my previous comment :
Sorry i forgot to answer that one. Yes the cube that has the script attached to it has a non-$$anonymous$$inematic rigidbody applied as well as the box collider. None of them is Trigger. However the script doesn't work.
I asked about the FPC because it doesn't have a Capsule Collider but i guess Character Controller is the same since it says there is one already when i try to add a Collider. I don't know what i am doing wrong. Is it possible that the engine itself is bugged?