Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 1.40 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using UnityEngine;
  2.  
  3. public class Grenade : MonoBehaviour {
  4.     public float lifeTime = 3f; // How long the nade should last, make sure it's less than the particle life-time of the grenade emitter.
  5.     public bool detachFX = true;
  6.     public Transform scorchFX;
  7.     float birthTime = 0f;
  8.     LayerMask ignoreMask;
  9.  
  10.     void Start() {
  11.         birthTime = Time.time; // Set time this grenade came into existance.
  12.         ignoreMask = 1 << 8;
  13.         ignoreMask = ~ignoreMask;
  14.     }
  15.  
  16.     void Update() {
  17.         if( Time.time - birthTime > lifeTime ) {
  18.             Destroy( gameObject ); // Kill this object if it's life time has expired.
  19.         }
  20.     }
  21.  
  22.     // Handle grenade collision.
  23.     void OnCollisionEnter( Collision collisionInfo ) {
  24.         if( detachFX ) {
  25.             foreach( Transform t in transform ) {
  26.                 if( t.name == "FX" )
  27.                     t.parent = null;
  28.             }
  29.         }
  30.  
  31.         SpawnScorchMark();
  32.         Destroy( gameObject );
  33.     }
  34.  
  35.     void SpawnScorchMark() {
  36.         Ray floorCheck = new Ray( transform.position, -Vector3.up );
  37.         RaycastHit hitInfo;
  38.         Physics.Raycast( floorCheck, out hitInfo, 10f, ignoreMask );
  39.  
  40.         if( hitInfo.collider ) {
  41.             Quaternion randomRotation = Quaternion.Euler( 0f, Random.Range( 0f, 360f ), 0f );
  42.             Instantiate( scorchFX, hitInfo.point, randomRotation );
  43.         }
  44.     }
  45. }