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
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 }
Yep, that’s definitely how it works. Is it displaying the right number now?