How to get an object to follow another object at a set distance in 2D

Hello Wise Ones!

I would like to get object B to follow object A at a set distance. I've attempted to modify the smoothfollow2d.js script like this:

var target : Transform;
var smoothTime = 0.3;
var xOffset : float = 1.0;
var yOffset : float = 1.0;

private var thisTransform : Transform;
private var velocity : Vector2;

function Start()
{
       thisTransform = transform;
}

function LateUpdate()
{

       thisTransform.position.x = Mathf.SmoothDamp( thisTransform.position.x, target.position.x, velocity.x, smoothTime) + xOffset;
       thisTransform.position.y = Mathf.SmoothDamp( thisTransform.position.y, target.position.y, velocity.y, smoothTime) + yOffset;

}

And it kind of works, but the object follow sort of 'jiggles' and if you look at the inspector, the following objects x, y values are constantly changing (by a very small amount), even though the target object is completely stationary.

I knew it wouldn't be a simple hack! If any of you clever boffins can help me, I'd be much obliged.

Judy x

you could try this:

    var target : Transform;
    var smoothTime = 0.3;
    var xOffset : float = 1.0;
    var yOffset : float = 1.0;

    private var thisTransform : Transform;
    private var velocity : Vector2;

    function Start()
    {
           thisTransform = transform;
    }

    function LateUpdate()
    {

           thisTransform.position.x = Mathf.Lerp( thisTransform.position.x, target.position.x + xOffset, Time.deltaTime * smoothTime);

           thisTransform.position.y = Mathf.Lerp( thisTransform.position.y, target.position.y + yOffset, Time.deltaTime * smoothTime);

    }

Thank you !
It’s very helpful!!

Just one question, you declare the private var velocity as a Vector2. What is the utility ?

Maybe you should also check the distanceJoint2D component instead of scripting away.