
Untitled
By: a guest on
May 4th, 2012 | syntax:
None | size: 1.40 KB | hits: 11 | expires: Never
using UnityEngine;
public class Grenade : MonoBehaviour {
public float lifeTime = 3f; // How long the nade should last, make sure it's less than the particle life-time of the grenade emitter.
public bool detachFX = true;
public Transform scorchFX;
float birthTime = 0f;
LayerMask ignoreMask;
void Start() {
birthTime = Time.time; // Set time this grenade came into existance.
ignoreMask = 1 << 8;
ignoreMask = ~ignoreMask;
}
void Update() {
if( Time.time - birthTime > lifeTime ) {
Destroy( gameObject ); // Kill this object if it's life time has expired.
}
}
// Handle grenade collision.
void OnCollisionEnter( Collision collisionInfo ) {
if( detachFX ) {
foreach( Transform t in transform ) {
if( t.name == "FX" )
t.parent = null;
}
}
SpawnScorchMark();
Destroy( gameObject );
}
void SpawnScorchMark() {
Ray floorCheck = new Ray( transform.position, -Vector3.up );
RaycastHit hitInfo;
Physics.Raycast( floorCheck, out hitInfo, 10f, ignoreMask );
if( hitInfo.collider ) {
Quaternion randomRotation = Quaternion.Euler( 0f, Random.Range( 0f, 360f ), 0f );
Instantiate( scorchFX, hitInfo.point, randomRotation );
}
}
}