Getting Parent object from a child's collision.
I am shooting a bullet at my characters and they have head and body parts. I want to do damage to the player as a whole and not to the individual body parts. I am currently using this script to get the id of the object I've collided with how can I do something similar to get the name of the parent of this object?
private void OnCollisionEnter(Collision collision)
{
//checks if the thing hit was a player or something else
if (collision.gameObject.tag == "Player")
{
//gets the body part hit
hitID = collision.collider.gameObject.name;
//part that i need help with vvvv
playerID = collision.collider.gameObject.name;
//calls the damage player method
CmdPlayerHit(hitID, playerID, damage);
//Destroys the projectile when it hits a player
Destroy(this.gameObject);
}
else
{
Destroy(this.gameObject);
}
}
Answer by Commoble · Mar 02, 2017 at 04:44 AM
collision.gameObject.transform.parent.gameObject
will give you the parent gameobject of the gameobject colliding with your script's object.
Your answer
Follow this Question
Related Questions
¿Why wont child move with parent? 0 Answers
How to get the child collider of your own player when there is a collision between two players. 0 Answers
Enable Multiple Box Collides From Parent Object 2 Answers
How to set an object to be a child of another using c# 1 Answer
OnParticleCollision() on parent script, for a child particle system? 2 Answers