OnExplosionEffect

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "</javascript>" to "</syntaxhighlight>")
(updated code)
Line 3: Line 3:
 
[[Category: JavaScript]]
 
[[Category: JavaScript]]
 
[[Category: ReallySimpleScripts]]
 
[[Category: ReallySimpleScripts]]
Author: Opless
 
 
 
== Description ==
 
== Description ==
  
 
Simple Explosion Effect.
 
Simple Explosion Effect.
 
Instantiate along with particles to throw nearby rigidbodies away from the explosion.
 
Instantiate along with particles to throw nearby rigidbodies away from the explosion.
 
== CSharp - OnExplosionEffect.cs ==
 
  
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
 
using UnityEngine;
 
using UnityEngine;
using System.Collections;
 
  
public class OnExplosionEffect : MonoBehaviour {
+
public class ExplosionForce : MonoBehaviour
+
{
public float radius = 5;
+
    [SerializeField] private float m_Radius = 5f;
public float power  = 5;
+
    [SerializeField] private float m_Power = 25f;
public float upwardForce = 0;
+
    [SerializeField] private float m_UpwardForce = 0f;
  
private float radiusUsed = 0.5F;
+
    // Automatic destruction delay.
 +
    [SerializeField] private float m_TimeOut = 1.0F;
  
// 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 ()
+
    private void Start ()
{
+
    {
Invoke ("DestroyNow", timeOut);
+
        Destroy (gameObject, m_TimeOut);
}
+
    }
  
void DestroyNow ()
 
{
 
  
DestroyObject (gameObject);
+
    private void FixedUpdate ()
}
+
    {
+
        // Applies an explosive force to all nearby rigidbodies.
+
        Vector3 explosionPos = transform.position;
}
+
        Collider [] colliders = Physics.OverlapSphere (explosionPos, m_Radius);
  
</syntaxhighlight>
+
        foreach (Collider hit in colliders)
 +
        {
 +
            if (hit == null)
 +
            {
 +
                continue;
 +
            }
  
== Javascript - OnExplosionEffect.js ==
+
            if (hit.attachedRigidbody)
 +
            {
 +
                hit.attachedRigidbody.AddExplosionForce (m_Power, explosionPos, m_Radius, m_UpwardForce);
 +
            }
 +
        }
 +
    }
  
<syntaxhighlight lang="javascript">
 
var radius : float = 5;
 
var power  : float = 5;
 
var upwardForce : float = 0;
 
  
private var radiusUsed : float = 0.5F;
+
    // Debug function to visualize explosion radius.
 
+
    private void OnDrawGizmos ()
// Update is called once per frame
+
    {
function FixedUpdate ()
+
        Gizmos.color = new Color (1, 0, 0, 0.3f);
{
+
        Gizmos.DrawSphere (transform.position, m_Radius);
// 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);
 
}
 
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 16:23, 6 September 2018

Description

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

using UnityEngine;
 
public class ExplosionForce : MonoBehaviour
{
    [SerializeField] private float m_Radius = 5f;
    [SerializeField] private float m_Power = 25f;
    [SerializeField] private float m_UpwardForce = 0f;
 
    // Automatic destruction delay.
    [SerializeField] private float m_TimeOut = 1.0F;
 
 
    private void Start ()
    {
        Destroy (gameObject, m_TimeOut);
    }
 
 
    private void FixedUpdate ()
    {
        // Applies an explosive force to all nearby rigidbodies.
        Vector3 explosionPos = transform.position;
        Collider [] colliders = Physics.OverlapSphere (explosionPos, m_Radius);
 
        foreach (Collider hit in colliders)
        {
            if (hit == null)
            {
                continue;
            }
 
            if (hit.attachedRigidbody)
            {
                hit.attachedRigidbody.AddExplosionForce (m_Power, explosionPos, m_Radius, m_UpwardForce);
            }
        }
    }
 
 
    // Debug function to visualize explosion radius.
    private void OnDrawGizmos ()
    {
        Gizmos.color = new Color (1, 0, 0, 0.3f);
        Gizmos.DrawSphere (transform.position, m_Radius);
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox