imagination XD

realizing dream

[EFTL] Some Legacy Script

Leave a comment

To create a script like a grenade will explode / damaging other object on certain radius.

// Explosive Object. For example OnColliderDamage.js
var explosionPrefab : Transform;
var damage : float;
var explosionPower = 5.0;
var radius = 6.0;

function OnCollisionEnter(col : Collision){
  Instantiate(explosionPrefab,transform.position,transform.rotation);
  Destroy(gameObject);
  var colliders = Physics.OverlapSphere(transform.position,radius);
  for(var hit in colliders){
      var body = hit.gameObject.rigidbody;
      if(body) hit.rigidbody.AddExplosionForce
(explosionPower,transform.position,radius,3.0);
      hit.SendMessageUpwards
("ApplyDamage",damage,SendMessageOptions.DontRequireReceiver);
  }
}
// to know how large the radius is.
function OnDrawGizmosSelected(){
    Gizmos.color = Color.cyan;
    Gizmos.DrawWireSphere(transform.position,radius);
}

and this is the script that attached to object to explode. 
For instance (DestructableObj.js)
var explosion : Transform;
var hitPoints = 100.0;

function ApplyDamage(damage : float){
    if(hitPoints <= 0.0) return;
    hitPoints -= damage;
    if(hitPoints <= 0.0) Explode();
}

function Explode(){
Instantiate(explosion,transform.position,transform.rotation);
Destroy(GameObject);
}

It’s the simple script that i use for explosion for Escape From the Laboratories and Catapult. You also can make the explosive type at OnCollisionDamage.js script by using enum. For instance

enum Explosive {TNT,Nitro}
var explosiveType : Explosive;
var timeOut = 3.0;

function OnCollisionEnter(col : Collision){
if(explosiveType == Explosive.TNT) PlayBeeps();
else if(explosiveType == Explosive.Nitro) Explode();
}

function PlayBeeps(){
    audio.clip = beep;
    if(audio && !audio.isPlaying) audio.Play();
    yield new WaitForSeconds (timeOut);
    HandlerFunc();
}

function HandlerFunc(){audio.Stop(); Explode();}
Advertisements

Author: IMGVERTEX

Started making games since 13 year old. 3D CGI Weeaboo | VFX Otaku | Anime Researcher | Indie Game Dev | VR Chuunibyou

FEED ME

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s