- Home /
Very simple collision death
Hi guys, I need a little help with scripting. I'm not a coder myself, and since I'm a one man team it means the progression to coding is slow. I'm doing a game jam with a very simple game, and so far its going very well. However, I've come across a bit of a problem. I can't figure out how to (and also cannot find)a script which would basically mean if anything collides with my players collider, then the player gets destroyed. It would need to attach to only the head part of my character (on its rig), which has of course a collider and rigibody of its own. If anyone could help that would be fantastic. I know I should be more knowledgeable about coding and that it's annoying to have people come to you with questions without any knowledge of what it means, I completely get that, but I am learning, its just slow.
Thanks guys
Answer by Nequium · Apr 05, 2014 at 04:59 PM
This answer assumes that your character is made up of a bunch of different pieces, considering it has a "head part." Make sure the other objects that collide with your player have a collider of their own. Set all colliders to "Is Trigger." Add rigidbodies to your player and all objects that can kill it. Make sure "Use Gravity" is unchecked on all rigidbodies (unless you want to use gravity).
Create a new C# script. Add this code to the script BELOW the Update function. NOT inside of it:
//Drag your MAIN player object into the variable below AFTER you've attached this script to your player's head public Transform player; void OnTriggerEnter(Collider other) { Destroy(player.gameobject); }
Attach the script to your player's head. This should do the trick, if I correctly understand they way you have your player set up, which is that he has different objects making up his body, all children of a main object. If your player is just ONE object, simply change Destroy(player.gameobject);
to Destroy(gameobject);
and get rid of the player Transform variable.
EDIT: Oh, I fixed those past errors, for anyone getting error messages, make sure to put the code just before the last closed bracket, so it has to be inside $$anonymous$$onobehaviour but not the others inside of that
Thanks so much for all your help mate, I really needed that! If you need any models being done, I'll surely give it a shot
Your answer
Follow this Question
Related Questions
Disable/Enable Colliders 1 Answer
No Collision 3 Answers
object goes through another object error 1 Answer
Do you have to have a rigidbody component for collision detection? 1 Answer
How to check if two objects are mutually ignoring collision? 0 Answers