# Replicate local movement between objects

Hi,
I’m trying to write a script to be added to a game object A that replicates the deltas (between frames) of local position and local rotation of another game object B.
For example when B moves to its right by say 1 unit, A moves to its right by 1 unit.
The same thing for rotation: when B rotates by 10 degrees on its left, A rotates by 10 degrees on its left.

In the script I keep the local position and rotation of the object in the previous frame and calculate the deltas in the current frame, I managed to make the rotation work, but I’m struggling with replicating the position.

``````public class MirrorLocalMovement : MonoBehaviour
{
public Transform target;

private Vector3 prevLocalPos;
private Quaternion prevLocalRot;
private bool firstUpdate = true;

private void Update()
{
if (firstUpdate)
{
firstUpdate = false;
}
else
{
transform.Rotate((target.localRotation * Quaternion.Inverse(prevLocalRot)).eulerAngles);

// Translation?

}

prevLocalPos = target.localPosition;
prevLocalRot = target.localRotation;
}
}
``````

I tried with transform.Translate and transform.InverseTransformDirection but with no success.
How can I replicate the translation in local space between the two objects?

Here a possible solution (tempTransform is a temporary empty transform to be set in inspector, used to maintain the previous rotation and call InverseTransformDirection):

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MirrorBehaviour : MonoBehaviour
{
public Transform target;
public Transform tempTransform;

private Vector3 prevPos;
private Quaternion prevRot;
private bool firstUpdate;

private void Update()
{
if (firstUpdate)
{
firstUpdate = false;
}
else
{
Vector3 deltaPos = target.localPosition - prevPos;
tempTransform.rotation = prevRot;
Vector3 localDir = tempTransform.InverseTransformDirection(deltaPos);
transform.Translate(localDir.x, localDir.y, localDir.z);

Quaternion deltaRot = target.localRotation * Quaternion.Inverse(prevRot);
Vector3 eulerDeltaRot = deltaRot.eulerAngles;
transform.Rotate(eulerDeltaRot.x, eulerDeltaRot.y, eulerDeltaRot.z);
}

prevPos = target.localPosition;
prevRot = target.localRotation;
}
}
``````