I'm implementing magnet like behavior in unity3d.
the red object have a script that add force to it toward a blue object.
to do that i use this code:
void FixedUpdate()
{
float distance = Vector2.Distance(blue.transform.position, transform.position);
speed = MAX_DISTANCE - distance;
body2d.AddForce((blue.transform.position - transform.position).normalized * speed);
}
MAX_DISTANCE
is maximum distance that red and blue object can have.
with this code i got this behavior :
but i don't want red object gets this far after reaching blue object i want red object to decrease it's speed and after maybe some shaking around blue object and eventually stop on blue object, how can i implement this?
UPDATE
red and blue objects can't collide with each other they just know their location.