OnExplosionEffect

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Added a Javascript Translation)
Line 1: Line 1:
 
[[Category: MonoBehaviour]]
 
[[Category: MonoBehaviour]]
 
[[Category: C Sharp]]
 
[[Category: C Sharp]]
 +
[[Category: JavaScript]]
 
[[Category: ReallySimpleScripts]]
 
[[Category: ReallySimpleScripts]]
 
 
Author: Opless
 
Author: Opless
  
Line 8: Line 8:
  
 
Simple Explosion Effect.
 
Simple Explosion Effect.
 +
Instantiate along with particles to throw nearby rigidbodies away from the explosion.
  
 
== CSharp - OnExplosionEffect.cs ==
 
== CSharp - OnExplosionEffect.cs ==
Line 60: Line 61:
  
 
</csharp>
 
</csharp>
 +
 +
== Javascript - OnExplosionEffect.js ==
 +
 +
<javascript>
 +
var radius : float = 5;
 +
var power  : float = 5;
 +
var upwardForce : float = 0;
 +
 +
private var radiusUsed : float = 0.5F;
 +
 +
// Update is called once per frame
 +
function FixedUpdate ()
 +
{
 +
// Applies an explosion force to all nearby rigidbodies
 +
var explosionPos : Vector3 = transform.position;
 +
var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
 +
 +
for (var hit : Collider in colliders)
 +
{
 +
if(hit == null)
 +
{
 +
continue;
 +
}
 +
if (hit.rigidbody)
 +
{
 +
hit.rigidbody.AddExplosionForce(power, explosionPos, radiusUsed, upwardForce);
 +
}
 +
}
 +
radiusUsed = ((radius-radiusUsed)/2)*Time.deltaTime;
 +
}
 +
 +
// Auto destroy
 +
var timeOut : float = 1.0F;
 +
 +
function  Awake ()
 +
{
 +
Invoke ("DestroyNow", timeOut);
 +
}
 +
 +
function DestroyNow ()
 +
{
 +
 +
DestroyObject (gameObject);
 +
}
 +
</javascript>

Revision as of 16:09, 26 November 2011

Author: Opless

Description

Simple Explosion Effect. Instantiate along with particles to throw nearby rigidbodies away from the explosion.

CSharp - OnExplosionEffect.cs

<csharp> using UnityEngine; using System.Collections;

public class OnExplosionEffect : MonoBehaviour {

public float radius = 5; public float power = 5; public float upwardForce = 0;

private float radiusUsed = 0.5F;

// Update is called once per frame void FixedUpdate () { // Applies an explosion force to all nearby rigidbodies Vector3 explosionPos = transform.position; Collider[] colliders = Physics.OverlapSphere (explosionPos, radius);

foreach (Collider hit in colliders) { if(hit == null) continue; if (hit.rigidbody) { hit.rigidbody.AddExplosionForce(power, explosionPos, radiusUsed, upwardForce); } } radiusUsed = ((radius-radiusUsed)/2)*Time.deltaTime; }

// Auto destroy public float timeOut = 1.0F;

void Awake () { Invoke ("DestroyNow", timeOut); }

void DestroyNow () {

DestroyObject (gameObject); }


}

</csharp>

Javascript - OnExplosionEffect.js

<javascript> var radius : float = 5; var power  : float = 5; var upwardForce : float = 0;

private var radiusUsed : float = 0.5F;

// Update is called once per frame function FixedUpdate () { // Applies an explosion force to all nearby rigidbodies var explosionPos : Vector3 = transform.position; var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);

for (var hit : Collider in colliders) { if(hit == null) { continue; } if (hit.rigidbody) { hit.rigidbody.AddExplosionForce(power, explosionPos, radiusUsed, upwardForce); } } radiusUsed = ((radius-radiusUsed)/2)*Time.deltaTime; }

// Auto destroy var timeOut : float = 1.0F;

function Awake () { Invoke ("DestroyNow", timeOut); }

function DestroyNow () {

DestroyObject (gameObject); } </javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox