same perspective view of 2 cubes with different position

Hello,


I´m developing a Profiler Tool in Unity VR and I have a problem where I need help. I will simplify my problem so it’s easier to understand.

I have two primitive cubes, let’s say cube A and Cube B. Cube A has always a Rotation of Vector3.zero. Cube B appears after a button click with a little distance in front of the Camera.

Now I want that Cube B has the same relative Rotation to my Main Camera (Camera that its attached to the head mounted display) as Cube A.

As Example: Imagine Cube A is at position (0,0,0) an the main Camera at (2,10,-10). Cube B (2,10,-9) is in front of the camera .
Now when I look at Cube B, I have the same “perspective view” as if I look at cube A.


One of my ideas was to calculate the DeltaRotation of the forward Vectors:

Vector3 lookDirectionCubeA = Camera.main.transform.position - cubeA.transform.position;
Vector3 lookDirectionCubeB = Camera.main.transform.position – cubeB.transform.position;
Quaternion deltaRotationCubeA = Quaternion.FromToRotation(CubeA.transform.forward, 
lookDirectionCubeA);

Quaternion deltaRotationCubeB = Quaternion.FromToRotation(cubeB.transform.forward, 
lookDirectionCubeB);

 Quaternion diff = deltaRotationCubeB * Quaternion.Inverse(deltaRotationCubeA);

And use the lookAt Method:

Vector3 forward = cubeB.transform.forward;    
forward = diff * forward;
cubeB.transform.LookAt(forward);

But that doesn’t seem to work.

Thanks for every idea and help :slight_smile:

Edit: For better understanding i took a view screenshots:

First img: Cube A and B have the same world Rotation.

Second Img: Now i want the cube B looks at the camera exact like cube A (I tried to rotate cube B in the Inspector for the screenshot)

If cube has 0 rotation, then it’s relative rotation from the camera would be Quaternion.Inverse(camera.transform.rotation), which is the rotation you would give Cube B. Unless my assumption about what you are looking for is wrong. Because you do not answer the question from where the camera would be looking at A

My solution so far: Create empty GameObject “G” as child on camera.

G.transform.LookAt(cubeA.transform.position);
GameObject copyOfCubeA = Instaniate(cubeA);
copyOfCubeA.transform.parent = G.transform;
G.transform.LookAt(cubeB.position);
copyOfCubeA.transform.localPosition = new Vector3(0,0,distanceToCubeB);
cubeB.transform.rotation = copyOfCubeA.transform.rotation;
Destroy(copyOfCubeA);

I’m sure there is another solution without making a copy of CubeA and only manipulating the rotation of CubeB. But it works now for me :slight_smile: