I’ve been looking through the forms for a few days now, and haven’t found anything which can point me in the right direction. I apologize in advance if this topic has been discussed.
I am a complete novice at Unity3d and have been making my way through tutorials and manual.
I have 4 objects in 2 groups, A1 and A2, B1 and B2. I know where A1 and A2 are, and I know where B1 is. I want to move B2 in relation to B1, to the same point as A2 in relation to A1 (A2 moves B2 should move). Group A may not pointed the same direction, and Group B(0.5) is half the size of Group A (1).
With the quick “sample” code below attached to A2, B2 moves correctly, but not in distance, like it’s on a tether.
Vector3 pos = A1.transform.position;
Vector3 dir = (this.transform.position - A1.transform.position).normalized;
Debug.DrawLine(pos, pos + dir, Color.red);
Vector3 dirScale = Vector3.Scale(dir, new Vector3(0.5f, 0.5f, 0.5f));
Vector3 posLocal = B1.transform.position;
Debug.DrawLine(posLocal, posLocal + dirScale, Color.green);
B2.transform.position = posLocal + dirScale;
Debug.Log("posLocal : " + dirScale);
Answer
This is the solution that works. Thank KillMobil for this answer.
float relativeScale = 0.5f;
float distanceAmount = Vector3.Distance(A1.transform.localPosition, this.transform.localPosition);
Vector3 distanceTo = Vector3.forward * distanceAmount;
Vector3 diff = (this.transform.localPosition - A1.transform.localPosition).normalized;
Quaternion rot = Quaternion.LookRotation(diff);
Vector3 resultPos = ConvertVector(distanceTo, rot, Vector3.zero, Vector3.one);
Vector3 posLocal = B1.transform.localPosition;
B2.transform.localPosition = (posLocal + resultPos) * relativeScale;
Debug.Log("[RESULTPOS] " + resultPos);