So before I start, I did try googling this a bit. And there’s really nothing that gets the effect right. Unless there’s an extra step I’m missing.
This is meant as a 2D game, but I’m using 3D physics with an orthographic camera.
Basically, I have this vortex on the screen. Just a sphere for now. When objects get near it, I want them to be pulled towards the center. The strength of the pulling force should be strongest near the center of the vortex and slowly decrease as you extend out.
If an object is moving fast enough, it should be able to avoid being sucked into the vortex (depending on how close it is to the center), but may have its path altered by the force of the pull.
It is possible to shoot another object through the center of the vortex with enough speed to potentially knock out the objects that are trapped in its center.
my code on my vortex is:
void FixedUpdate()
{
Vector3 explosionPos = thisTransform.position;
Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
foreach(Collider hit in colliders)
{
if(hit && hit.transform != thisTransform && hit.rigidbody)
{
Vector3 difference = hit.transform.position - thisTransform.position;
hit.rigidbody.AddForce(difference.normalized * power, ForceMode.Force);
}
}
}
My objects are spheres with sphere colliders and rigidbodies. They are initially launched by setting rigidbody.velocity.
My vortex is a sphere with a sphere collider set as a trigger (to avoid actual objects colliding with the vortex itself)
This almost works… but it causes more of an orbit type of effect, that eventually launches the objects out on its own. I’m assuming the amount of force that gets added ends up pushing the object passed the center, causing the opposite force to apply on the next check, making it sort of ping pong back and forth.
Any ideas?