- Home /
How to apply damage without Raycast
Hi there! Sorry if this question may look inappropriate but I can't find a solution on my own. First let me try to explain you what I'd like to do: I'd like my enemy to decrease its life of an amount called damage, specified by the bullet with which it collides.
I'm using instantiate and prefabs to handle the shootings, not Raycast, so I can't benefit from RaycastHit. All my bullets are prefab that player can instantiate.
So right now I'm checking for collisions with OnTriggerEnter, and then I'm applying the damage. If I had just one kind of bullet that would be done... but I'm planning to add more bullets to my game, so it'd be painful to write a specific OnTriggerEnter for every one of them, considering that I'd also have to access the damage of every bullet.
How can I do to make my enemy recognize which bullet hit it, so that it automatically decrease the right amount of health on every hit? I can't help myself thinking that this must be something obvious...
Here is a part of my enemy script:
void OnTriggerEnter(Collider collider){
if(collider.gameObject.CompareTag("Shot")){
AdjustHealth(??????); //I'd like to put in those brackets the current bullet's damage
}
}
void AdjustHealth(float adj){
currentHealth += adj;
if(currentHealth <= 0)
Destroy(this.gameObject);
}
}
Code is appreciated, but I'd prefer you to point me in the right direction instead of just feeding me with the solution... thanks for your time!
You could just check if the collider contains the damage variable and use that assu$$anonymous$$g that the variable is always named the same. Something like
void OnTriggerEnter(Collider collider){
if(collider.gameObject.CompareTag("Shot")){
bulletScript script = collider.gameObject.GetComponent<BullectScript>();
if(script!=null){
AdjustHealth(script.damage);
}
}
}
should work just fine assu$$anonymous$$g that the damage variable is public. If not - call a method that returns the value.
Hi ShadoX and thanks for your comment! Let's see if I've understood correctly: that way I won't need to reiterate anything when the enemy collides with another bullet? I just have to give all my bullets the same variable " damage"?
Upvoted for expressing eagerness to learn over getting a complete solution.
@esitoinatteso Yup, at last as long as you use the same script on all bullets and just modify the damage variable.
Answer by whydoidoit · Oct 11, 2013 at 04:48 PM
Just send a message to the other object from the bullet, if it has an AdjustHealth function it will be called:
public float damage = 10;
void OnTriggerEnter(Collider otherCollider)
{
otherCollider.SendMessage("AdjustHealth", damage, SendMessageOptions.DontRequireReceiver);
}
That seems just what I was looking for! Unfortunately I've messed up with the project I was working and I can't test it right now, but as soon as I can, I'll try that and update your kind answer as THE answer if it turns out to work as intended... meanwhile thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613115742im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Copy a damage value on collision 2 Answers
Damage on collision with player.. 1 Answer
Collision, tags, and triggers 1 Answer
This collision Enemy health code suddenly stopped working. 1 Answer
Damage over time while colliding. 1 Answer