- Home /
Modify AddExplosionForce and allow upwardsModifier to any direction instead
Is there any easy way to implement this or do we need to do it manually? Anyone happen to already know of some good implementation of this?
I want something like to this:
function AddExplosionForceModifier (explosionForce : float, explosionPosition : Vector3, explosionRadius : float, explosionOriginPoint : Vector3 = new Vector3(0.0F,0.0F,0.0F), mode : ForceMode = ForceMode.Force) : void
So I can point the explosionOriginPoint (rather than a directionModifier or upwardsModifier) to anywhere and not just up or down.
I'm using AddExplosionForce for wind emulation, and it works great to go up. But now I need new directions.
edit: I just noted this question could also be asked in another way which will hopefully facilitate me finding the answer on my own:
How is the code implementation on AddExplosionForce?
Answer by cregox · Feb 01, 2011 at 12:15 AM
Here, I just implemented it myself... Tho it's still far from perfect, it simulates the behavior I need for this moment.
Just create a RigidbodyExtensions.cs anywhere inside the unity project and add this:
using UnityEngine; using System.Collections;
public static class RigidbodyExtensions : object { public static void AddExplosionForce (this Rigidbody body, float explosionForce, Vector3 explosionRadiusCenter, float explosionRadius) { AddExplosionForce(body, explosionForce, explosionRadiusCenter, explosionRadius, new Vector3(0F, 0F, 0F)); } public static void AddExplosionForce (this Rigidbody body, float explosionForce, Vector3 explosionRadiusCenter, float explosionRadius, Vector3 explosionOriginPoint) { AddExplosionForce(body, explosionForce, explosionRadiusCenter, explosionRadius, explosionOriginPoint, ForceMode.Force); } public static void AddExplosionForce ( // still missing torque - try using "AddForceAtPosition" later on this Rigidbody body , float explosionForce , Vector3 explosionRadiusCenter , float explosionRadius , Vector3 explosionOriginPoint // this is the oposite from upwardsModifier , ForceMode mode ) { if (Vector3.Distance(body.transform.position, explosionRadiusCenter) <= explosionRadius) { Vector3 force = (body.transform.position - (explosionRadiusCenter + explosionOriginPoint)); body.AddForce(force * (explosionForce/5), mode); // 5 came from experimentation } } }
here's a comparison usage:
rigidbody.AddExplosionForce(2000F, transform.position, 2F, 3f); // unity's
rigidbody.AddExplosionForce(2000F, transform.position, 2F, -(new Vector3(0F, 3F, 0F))); // cawas
note the **-** sign. it's needed for similar behavior.
Answer by 3DMagicVR · Jan 31, 2011 at 07:39 PM
I think you must play around with the explosionPosition and with the explosionOriginPoint if it's part of the function, because if it's not I don't think that works.
I think you missed my point. The function doesn't have it as Vector3, it's a float point. Take a look at it. ;)
Answer by Goody! · Jan 31, 2011 at 07:48 PM
Do a search for add.force and constant.force. I believe you can specify direction with those. You can also set them to be local to the object of local to the world.
The major point of explosionforce is that it's applied as the name imply - an explosion. If the object is 3 feets away but within the explosion radius it will be less affected than an object in the center of the explosion.
Your answer