Use force to move to position of object

Hello there.

What I’m looking to achieve, is to make a rigidbody follow another gameobjects position, and I want the rigidbody to travel faster if the distance between the rigidbody and the gameobject is less.

I tried using a spring joint, but I realized that I need it to work the opposite way as a spring joint, so instead it would move faster the closer to the connected body the rigidbody is.

I’ve experimented with configurable joint, but I can’t seem to understand how to do what I want with that method.

I’ve been looking into adding constant force directly in the direction of the object:

Vector3 Direction = (GameobjectA.transform.position - gameObject.transform.position).normalized;
rigidbody.constantForce.force = Direction;

I’ve been trying to modify the rigidbody.velocity directly, but I realized that I should not do that, as I need my physics objects to work correctly

So at the moment I’m wondering what method I should try and use. I’m not very experienced with maths and physics, and I’ve currently run out of options to try. I think adding force in the direction of the gameobject would be the way to go, but with what I have so far the rigidbody sort of bounces forth and back trying to reach the target position, and I’m not sure how to fix that without modifying the velocity directly or slow the rigidbody down when its closer, as I need the opposite to happen.

Note that my gameobject is empty, and my rigidbody is simply trying to reach its position with force.

Any help or ideas would be greatly appreciated, thanks in advance.

I’m not sure what you are going for here. You can modify the velocity based on distance. Here is a bit of code that, when the target is in range, chases the target. The closer it is, the faster it goes:

var target : Transform;
var maxDist = 5.0;
var maxVelocity = 8.0;

function FixedUpdate () {
	var dist = Vector3.Distance(target.position, transform.position);
    if (dist < maxDist) {
    	rigidbody.velocity = (target.transform.position - transform.position).normalized	 * (maxVelocity * (1.0 - dist / maxDist));
    }	
    else {
    	rigidbody.velocity = Vector3.zero;
    }
}