Local Gravity (Black Hole)

Hi All,

Wonder if someone can have a look at my script and tell me if they can see a problem.
I want to create a black hole effect around a sprite. The player has a rigidbody and mesh collider attached and the black hole has a sphere collider (trigger) attached and then this script.

#pragma strict
   
var radius = 5.0;

var power = -10.0;

function Update () {

    // 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)

            continue;

        

        if (hit.rigidbody)

            hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0);

    }

}

Thanks in advance for any pointers.

you need to add direction at first glance… This could be done through like this (this is sudo as I don’t actually know how to script in javascropt (i used C#)

if(hit.rigidbody)
{
   vector3 expDirection = hit.rigidbody.loc - explosionPos;
   expDirection = expDirection.normalized;
   hit.rigidbody.addForce(forceAmnt * (1/radius)*(1/radius) * time *expDirection)
}

When in doubt, don’t use inbuilt code, cus you will use it wrong… I’de recomend looking up the paramaters of real gravity… What ive done is very crude and not physically accurate… but it would get the job done atleast…

so yeah, maybe don’t use inbuilt code of the engine, make your own, that way you have full control… But to do that you will probably have to learn C# as I don’t know how well you will get along using javascript