2d - sprite following sprite

Can someone help me how can i make an object(sprite) to follow another sprite in unity?
What i want exactly:
if the first sprite goes up, then the second follow it, if it’s going down, the same.

thanks,
Andrei

1 Like

You could just add the two into the scene and drag one (the one following) under the object of the other in the Hierachy

Or use a script to make it more fluid and smooth have a look into Vector3.Lerp()

2 Likes
Public GameObject target;

Public float smoothing;

Private Vector3 initialPosition;

Private Vector3 targetPosition;

Void Start(){
initialPosition = target.transform.position;
}

Void Update(){

targetPosition = new Vector3(transform.position.x + (target.transform.position.x - initialPosition.x), transform.position.y + (target.transform.position.y - initialPosition.y), transform.position.z);

transform.position = Vector3.Lerp(transform.position, targetPosition, smoothing);

}

Set your target to whatever GameObject you want to follow, set your smoothing between 1 and 0 (1 is no smoothing, 0 is no movement). When the first GameObject moves, this should move the second one by the same amount in the same direction.

This code isn’t perfect, but should give you and idea of how this works.