Unity Projectile -2- Let's add effects to the missile.

This time, we will add FX effects when the missile is fired and when it collides with something. 


 


 

 

-Missile.cs Script -

 

 public class Missile : MonoBehaviour
 {
     Rigidbody rb;

     public float force = 10f;

     //Add
     public GameObject muzzleFx;
     //Add
     public GameObject destroyFx;

     private void Awake()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     private void OnEnable()
     {
         //Shot Projectile.
         rb.AddForce(transform.forward * force, ForceMode.VelocityChange);

         //Add Muzzle Fx.
         Instantiate(muzzleFx, transform.position, transform.rotation);
     }

     private void OnTriggerEnter(Collider other)
     {
         //Add Destory Fx.
         Instantiate(destroyFx, transform.position, transform.rotation);

         //Add Destroy missile.
         Destroy(gameObject);
     }
 }
 

 

 

 privatevoid OnTriggerEnter(Collider other)
{
    Instantiate(destroyFx, transform.position, transform.rotation);
    Destroy(gameObject);
}

This method is called when the missile collides with another collider. It instantiates the destruction effect at the missile's position and then destroys the missile GameObject.


- Missile Prefab Setting -

 

Conclusion

This script effectively manages the missile's behavior, including its movement, effects during firing, and destruction upon collision. By using Unity's physics system and GameObject instantiation, it creates a dynamic gameplay experience.

 
Previous Post Next Post