I have two objects in my scene each at different positions and rotations in the scene. In this example, object A (the leader) moves according to controls. I need object B (follower) to follow the leader's movement, but in its own space, and I'm having a ton of trouble with it.
For example, if the Leader moves 1 unit in its forward direction, I want the follower to move one unit in its own forward direction even if the two are facing different ways. The same goes for the other axes as well (Y/up/down works fine though).
I have tried seemingly every combination of Transform.Translate on the follower, but still, the forward/back/left/right axes get mixed up. It moves correctly when they're both facing the same way, but when the rotation changes (particularly on the Y-axis) things get messy. Here's my code at the moment. Any help would be appreciated:
private void SetStartPos()
{
// these are two cached Vector3 variables
startPos = leader.localPosition;
startRot = leader.eulerAngles;
}
private void Update()
{
Vector3 posOffset = leader.localPosition - startPos;
Vector3 rotOffset = leader.eulerAngles - startRot;
SetStartPos();
// because this runs in update, this check just prevents huge
// instant offset glitches at the beginning.
if (posOffset.x > 3 || posOffset.y > 3 || posOffset.z > 3 || rotOffset.y > 5f)
return;
follower.transform.eulerAngles += rotOffset;
follower.transform.Translate(posOffset, Space.Self);
}
To clarify, if needed, the goal is to get them to move by the same amount in the same direction relative to themselves. For some reference, this is being used for matching an AR device's 3D position to the position of a GameObject being streamed over a local network for a basic tracking setup.
Thank you very much in advance!
UPDATE: After doing some more digging, I did find a solution that works for the tracking/following, but there seems to be an issue where it immediately jumps to a seemingly arbitrary position and rotation when it begins. And, if I disable track
, then reenable it, the follower will snap to the rotation of the leader, rather than continuing from it's current rotation as needed.
private Vector3 leaderStart, followerStart;
private Quaternion rotationDiff;
private bool isFirstTrack = false;
private bool track = true;
private void SetStartPos()
{
leaderStart = leader.localPosition;
followerStart = follower.position;
}
void CalculateObjectDiff()
{
followerStart = follower.position;
Quaternion origYRot = Quaternion.Euler(0, follower.rotation.eulerAngles.y, 0);
Quaternion origProxyRot = Quaternion.Euler(0, leader.rotation.eulerAngles.y, 0);
rotationDiff = origYRot * Quaternion.Inverse(origProxyRot);
}
private void Update()
{
if (!track)
return;
if (isFirstTrack)
{
SetStartPos();
CalculateObjectDiff();
isFirstTrack = false;
return;
}
follower.position = followerStart + rotationDiff * (leader.position - leaderStart);
follower.rotation = rotationDiff * leader.rotation;
}
isFirstTrack
when changingtrack
fromfalse
totrue
? \$\endgroup\$