- Home /
How i destroy the object of that part that collides with another object?
I am trying to do that my player(cube) is moving when it collides with the another cube e.g my player collides at the corner of another cube I want to destroy the the player part with another cube part collides
Hi, write some code using OnCollisionEnter() and if you have trouble post it as a question. https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
Answer by lucasdvazquez7 · May 11, 2020 at 04:01 AM
I don't quite understand what you wanted to do.
What I understood was that you wanted to destroy the gameobject that your player collided with. To do that, you need to reference that gameobject in your player's script. For example:
//You have to drag your enemy prefab here
public GameObject enemy;
//First: you have to add a Box Collider to your cube
//Second: you have to create a tag to your enemy so that the player identifies it when it collides with it
//Third: destroy the object you collided with
void OnTriggerEnter(Collider other){
if(other.tag == "enemy"){
Destroy(enemy);
}
}
Hope this helps
I am saying that my player that is cube left side collides with another cube I want to destroy the Left side of my player
Answer by UnityToMakeMoney · May 11, 2020 at 04:36 AM
First off, the object needs to be a collection of other objects. If they are not a collection, then make a script so that they are always together.
Second, all you have to do now is tag each object in that collection so that when you collide with it, it gets destroyed. A good example of a collection of objects in a game would be like Snake, where each limb is attached to the head.
I am saying that my player that is cube left side collides with another cube I want to destroy the Left side of my player
Yes, that is what I am saying. make a player so that it looks like 1 object, but it is 2+ objects being held together. That way, when a specific part of a player is touched, you can destroy, hide, or whatever you want to do when you collide with it.
But I didn't know that left side collides or right side mid it depends how the user play