Last active
February 23, 2017 06:21
-
-
Save Domiii/34e4b2c02bed214dab3f4df51135ecfa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class Explosion2D : MonoBehaviour { | |
public float power = 10; | |
public float radius = 10; | |
void OnMouseDown() { | |
Explode (transform.position, radius, power); | |
} | |
public static void Explode(Vector3 sourcePos, float radius, float power) { | |
var objs = Physics2D.OverlapCircleAll (sourcePos, radius); | |
foreach (var obj in objs) { | |
var body = obj.GetComponent<Rigidbody2D> (); | |
if (body != null) { | |
AddForce(body, sourcePos, power); | |
} | |
} | |
} | |
public static void AddForce(Rigidbody2D body, Vector3 sourcePos, float power) { | |
var f = body.transform.position - sourcePos; | |
f.Normalize (); | |
f *= power; | |
body.AddForce (f, ForceMode2D.Impulse); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment