I want to have one “particle” attract other particles to it. So to test it, I created two spheres. One of which I added the following code to:
[RequireComponent(typeof(Rigidbody))]
public class Attraction : MonoBehaviour
{
public float pullRadius = 2;
public float pullForce = 1;
public void FixedUpdate()
{
foreach (Collider collider in Physics.OverlapSphere(transform.position, pullRadius)) {
// calculate direction from target to me
Vector3 forceDirection = transform.position - collider.transform.position;
// apply force on target towards me
collider.GetComponent<Rigidbody>().AddForce(forceDirection.normalized * pullForce * Time.fixedDeltaTime);
}
}
}
But what ends up happening is nothing. I tried making the radius of pull greater, and the other sphere wouldn’t move toward the initial one, and I have no idea why. It just didn’t move. Help!