Follow Script

When I am dragging one object, I want another object in the scene to move as well. At the moment I have pretty simple scripts attached. Here is the one on the object that drags:

function Update(){
	StaticVarDisc.PosX = transform.position.x; //this is defining posx etc
	StaticVarDisc.PosY = transform.position.y;
	StaticVarDisc.PosZ = transform.position.z;
    if (drag){
        var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        var distance: float;
        if (plane.Raycast(ray, distance)){
            transform.position = ray.GetPoint(distance);
        }
    }
}

and here is the code attached to the object I want to follow:

function Update () {
	transform.position.x = StaticVar.PosX; //this is calling posx etc
	transform.position.y = StaticVar.PosY;
	transform.position.z = StaticVar.PosZ;
}

This code works, but there is a slight delay for the other object to catch up (very small, but noticeable). If I drag the object fast enough, I can actually get them to overlap. I want them to appear more solid though as if they were a single object.

I understand there will always be delay, but could anyone suggest an alternative to make it update a bit faster to appear more solid and move as one object rather than two?

I think.its because tje follow script updates before te other script. and will have the old values of the position.

Try to put in the direct refference to the position

Make the second object a child of the first. You might want to move it to the desired position first, though, as it will stay there until you unparent them.

Forgot to add that the object will move to relative space instead of world space so you’ll have to be more careful when moving it independently in the future.

Use the transform.parent property to accomplish this.