RaycastHit

I am using Raycasts when shooting to send messages to the enemy in order to inflict damage.
If the RaycastHit detects an object with the tag “Zombie”, it sends a message “ShotByRay”. Under the ShotByRay() method on the zombie, I want to decipher WHERE the message came from, which will be used to determine how much damage is dealt. Yes technically I could write a different message for each weapon, but I was just wondering if there was a way to write some kind of if statement that looks for either the name or tag of the origin of the message. Any feedback is appreciated, thanks!

if (Physics.Raycast(transform.position, transform.forward, out hit, 300))
{
    if (hit.gameobject.tag=="Zombie")
        hit.GetComponent<Zombie>().ShotByRay(gameObject); // report myself to the zombie
}

A better approach would be to have a method on your Zombie that inflicts damage. This way the Zombie doesn’t need to know about all the different weapons. Instead the weapon determines the amount of damage.

if (Physics.Raycast(transform.position, transform.forward, out hit, 300))
{
    if (hit.gameobject.tag=="Zombie")
        hit.GetComponent<Zombie>().DoDamage(100); // inflict 100 damage
}
1 Like

Awesome, thank you

Exactly. That’s why most games have a “damage system” that includes details about the damage type and damage value so the damage receiver again has a chance to decide how it affects him. For example if you are in a typical fantasy setting with different damage types like fire, ice, generic, … it would mean a “fire” enemy may take less damage if the damage type is fire and more damage if the damage type is ice. Though including some other generic metadata like the damage dealer doesn’t hurt. Most damage receivers may not need it, but if they have a special ability to reduce close-range damage for example, they need to know the distance to the damage source. However all the base stats are of course determined by the weapon. So if a weapon deals more damage when it’s a close-range attack it would precalculate that and include the result in the reported damage value. So it doesn’t hurt to include more details in your message signature. That makes it more future proof. However if you really know that you don’t want to do any of that, don’t spend too much time on those aspects. In the end everything can be refactored if it’s important enough :slight_smile:

3 Likes

Thank you, I didn’t know it :slight_smile: