- Home /
Child object collision
I have a ship with multiple pieces of wood like a real ship. each piece has a collider and the same script:
public void ApplyDamage(int damageTotal)
{
health -= damageTotal;
Debug.Log("Damage " + damageTotal + " was applied");
Destroy(gameObject);
}
My goal is to check which piece got hit with a cannonball using send message or any other way. how do i get it to send message to the right piece.
Cannonball script:
void OnCollisionEnter(Collision hit)
{
Debug.Log("Collision Detected");
if (hit.gameObject.tag == "ShipPlate")
{
hit.gameObject.SendMessage("ApplyDamage", 5); // send the message to its owner
Debug.Log("aaa");
Destroy(gameObject);
}
}
Answer by razzraziel · Nov 10, 2020 at 05:53 PM
You should raycast and then use its hit property to determine which object gets hit by raycast. Don't use physics collisions, they probably cost more and most of the times are not accurate because of fixed update.
Also if it doesnt work correct, you possibly put script on wrong object (not pieces, on cannon).
The cannons are physics based projectiles though. I'm new to program$$anonymous$$g so how would you go about doing that?
**Instead of searching for the tag just use the name.**
if (hit.gameObject.name == "ShipPlate 1")
{
hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
Debug.Log("aaa");
Destroy(gameObject);
}
if (hit.gameObject.name == "ShipPlate 2")
{
hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
Debug.Log("aaa");
Destroy(gameObject);
}
**or you can do switch/case**
switch (hit.gameObject.name)
{
case "Ship 1":
hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
Debug.Log("aaa");
Destroy(gameObject);
break;
case "Ship 2":
hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
Debug.Log("aaa");
Destroy(gameObject);
break;
default:
Debug.Log("Error/ Couldnt find ship parts");
break;
}
}
I tried what you said and it didn't ,work this is my new script:
void OnCollisionEnter(Collision hit)
{
Debug.Log("aaa");
if (hit.gameObject.name == "3,3(Right)")
{
hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
Debug.Log("aaa");
Destroy(gameObject);
}
else if (hit.gameObject.name == "4,3(Right)")
{
hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
Debug.Log("aaa");
Destroy(gameObject);
}
else if (hit.gameObject.name == "4,2(Right)")
{
hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
Debug.Log("aaa");
Destroy(gameObject);
}
else if (hit.gameObject.name == "3,1(Right)")
{
hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
Debug.Log("aaa");
Destroy(gameObject);
}
else if (hit.gameObject.name == "3,2(Right)")
{
hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
Debug.Log("aaa");
Destroy(gameObject);
}
else if (hit.gameObject.name == "3,2(Right)")
{
hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
Debug.Log("aaa");
Destroy(gameObject);
}
else if (hit.gameObject.name == "Ship")
{
hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
Debug.Log("hit ship as whole not child");
Destroy(gameObject);
}
sadly it only returns the last one. How do i get it to collide with the child objects?
Answer by MurphyMurph_21 · Nov 13, 2020 at 03:24 PM
@Madkrumper9 I wouldnt use else if I would just use if statements for all of them. Do you have colliders on each child gameobject or just the main ship?
each child game object. though this still isn't working it registers a collision but doesn't do anything past that