Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ProjectileScript : MonoBehaviour
  5. {
  6.     public GameObject impactParticle;
  7.     public GameObject projectileParticle;
  8.     public GameObject muzzleParticle;
  9.     public GameObject[] trailParticles;
  10.     [HideInInspector]
  11.     public Vector3 impactNormal; //Used to rotate impactparticle.
  12.  
  13.     private bool hasCollided = false;
  14.  
  15.     void Start()
  16.     {
  17.         projectileParticle = Instantiate(projectileParticle, transform.position, transform.rotation) as GameObject;
  18.         projectileParticle.transform.parent = transform;
  19.         if (muzzleParticle){
  20.             muzzleParticle = Instantiate(muzzleParticle, transform.position, transform.rotation) as GameObject;
  21.             muzzleParticle.transform.rotation = transform.rotation * Quaternion.Euler(180, 0, 0);
  22.             Destroy(muzzleParticle, 1.5f); // Lifetime of muzzle effect.
  23.         }
  24.     }
  25.  
  26.     void OnCollisionEnter(Collision hit)
  27.     {
  28.         if (!hasCollided)
  29.         {
  30.             hasCollided = true;
  31.             //transform.DetachChildren();
  32.             impactParticle = Instantiate(impactParticle, transform.position, Quaternion.FromToRotation(Vector3.up, impactNormal)) as GameObject;
  33.             //Debug.DrawRay(hit.contacts[0].point, hit.contacts[0].normal * 1, Color.yellow);
  34.  
  35.             if (hit.gameObject.tag == "Destructible") // Projectile will destroy objects tagged as Destructible
  36.             {
  37.                 Destroy(hit.gameObject);
  38.             }
  39.  
  40.  
  41.             //yield WaitForSeconds (0.05);
  42.             foreach (GameObject trail in trailParticles)
  43.             {
  44.                 GameObject curTrail = transform.Find(projectileParticle.name + "/" + trail.name).gameObject;
  45.                 curTrail.transform.parent = null;
  46.                 Destroy(curTrail, 3f);
  47.             }
  48.             Destroy(projectileParticle, 3f);
  49.             Destroy(impactParticle, 5f);
  50.             Destroy(gameObject);
  51.             //projectileParticle.Stop();
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement