- Home /
How to make my enemy bleed
i want it so when i shoot an enemy it will create a particle that is played once everytime it is shot so it looks like blood but i cannot seem to find any tutorials on how to do this? i know how to create a particle that is played once but i don't know how to script it
Answer by aldonaletto · Oct 23, 2011 at 01:37 AM
Create the blood particles to splash blood upwards, set OneShot and Auto Destruct, and drag it to the Project panel to make it a prefab. If you are shooting the enemy with Raycast, instantiate the particles in the shooting script this way:
var bloodPrefab: GameObject; // drag the blood prefab here
... if (Physics.Raycast(ray, hit)){ // when you shoot... if (hit.transform.tag == "Enemy"){ // and hit an enemy... // find the necessary rotation... var rot = Quaternion.FromToRotation(Vector3.up, hit.normal); Instantiate(bloodPrefab, hit.point, rot); // and make the enemy bleed } } But if you are hitting the enemy with instantiated projectiles, add this code to the projectile script:
var bloodPrefab: GameObject; // drag the blood prefab here
function OnCollisionEnter(col: Collision){ if (col.transform.tag == "Enemy"){ var contact = col.contacts[0]; // get the first contact point info // find the necessary rotation... var rot = Quaternion.FromToRotation(Vector3.up, contact.normal); Instantiate(bloodPrefab, contact.point, rot); // and make the enemy bleed } Destroy(gameObject); // destroy the projectile }
so i would attach the projectile script to my bullet so when it hits the enemy it will play the particle?
Yes. If you are using a projectile to hit the enemy, attach the script above to the projectile prefab. When the projectile hits the enemy, it instantiates the blood particle at the contact point and destroy itself. You must set Auto Destruct and One Shot to true in the blood particle prefab, or else the blood will continue splashing in the same world point even after the enemy has moved to another place.
Thank you so much i got it to work but when your a certain distance away it wont play the particle but that is not a big problem and i can deal with that its a really good script thanks for your help!!! :)
[$$anonymous$$eta] is this question displaying "0" in the front page Answers counter for you guys as well? Looks like a QATO bug to me. O_O
@Christian H Pedersen, it's a QATO bug, for sure. I saw other questions displaying 0 answers as well, and even an answered question displaying -1 answers!
Answer by syclamoth · Oct 25, 2011 at 10:57 AM
Yep, that's definitely how it works. Is it displaying the right number now?