- Home /
OnCollisionEnter issues
I cannot for the life of me figure out what I am doing wrong. I have gone through several different tutorials and walkthroughs, but nothing seems to work. I want a "Ceiling" to eventually crush the "Player" if they don't make it to the exit in time. The player is tagged as "Player" and has a kinematic rigidbody attached. The ceiling has the following script attached.
function OnCollisionEnter (collision : Collision) {
if(collision.gameObject.tag == "Player"){
Destroy(gameObject);
}
}
Can anyone help me out with this? I know it has to be something simple...
Answer by Aridez · Jul 13, 2015 at 01:37 AM
I understand that you want to destroy the player object and you said that "The ceiling has the following script attached", so when you do "Destroy(gameObject);" you are destroying the ceiling (if the code works). Change it to "Destroy(collision.gameObject);" and that should work, let me know if that solves the issue!
I'm not sure what I'm doing wrong, but everything I've tried just isn't working. :( It seems like no matter what I do, the ceiling just passes right through my character...
Did you try the changes I posted above? If the ceiling is passing trhough your character maybe something is wrong with the set up of colliders (they should at least collide between them), how did you set up the scene?
I made the changes and it didn't effect anything :/ Here is the Inspector panel for my player and the ceiling, maybe you can see something that I can't... (Also, I do 'collide' with the ceiling. It stops my player, but it just isn't killing the player..)
are you sure that the ceiling layer is allowed to collide with the player layer from Edit>Project settings>Physics>matrix ? From your Picture both objects are in the same layer, however the layer can be set not to collide with itself.
I found out what could it be here: "You can move a kinematic rigidbody object from a script by modifying its Transform Component but it will not respond to collisions and forces like a non-kinematic rigidbody."
Looking at the chart on the "collision action matrix" at the bottom of the page linked we can see how collisions between "$$anonymous$$inematic Rigidbody Collider" and "Static Collider" are not detected.
You can either make the rigidbody you have non kinematic (unchecking it in the inspector) or you can add a rigidbody to the ceiling.
Answer by nadhimali · Jul 13, 2015 at 11:00 AM
This code will destroy the ceiling and also your code. you found the player by tag but the destroy action will not be applied to that player unless you correctly specify it.
function OnCollisionEnter (collision : Collision) {
if(collision.gameObject.tag == "Player"){
Destroy(collision.gameObject);
}
}
Answer by TruffelsAndOranges · Jul 14, 2015 at 10:48 PM
Destroy does NOT destroy the gameObject immedietly, it just tells the engine to destroy the game object whenever the engine wants to!
Try this instead:
gameObject.SetActive(false);
Destroy(gameObject);
Your answer
Follow this Question
Related Questions
Cant Find Destruction Code? 1 Answer
How to prevent losing multiple lives at zero health? 2 Answers
Help with player death on collision! 2 Answers
How to detect player collision 0 Answers