- Home /
How can my character be destroyed upon contact with 1 specific object?
Hi, i this is my first day with Unity and i am trying to build a little game (you have to steer between a lot of cubes). Right now nothing happens when i hit the cubes, but i want it so my character get's destroyed when i hit these cubes. How can i do this?
Answer by whebert · Jun 14, 2013 at 09:52 PM
You can use OnCollisionEnter, assuming your player and the cubes have Colliders and at least one has a non-kinematic RigidBody attached. Assuming your Cubes had say a "Cube" behavior attached to each of them, this may work (untested):
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.GetComponent<Cube>())
{
Destroy(this);
// Or do something that means "destroyed" in your game.
}
}
Edit: You're probably using a CharacterController for your player, yes? In which case you can use (untested):
void OnControllerColliderHit(ControllerColliderHit hit)
{
if(hit.gameObject.GetComponent<Cube>())
{
Destroy(this);
// Or whatever you wanna do
}
}
Your answer
Follow this Question
Related Questions
destruction on impact 2 Answers
Change scene using Colliders 1 Answer
Unable to get OnTriggerEnter to work.[JS] 2 Answers
Game object collapsing/ going out of shape on impact from other objects. 0 Answers