- Home /
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 :)
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)
I haven't really looked through your code. However I can see one thing that makes no sense, specifically this line:
cubeB.transform.LookAt(forward);
The LookAt method takes a world space position as argument, not a direction. It works different from the LookRotation method in the Quaternion struct. The LookAt method is most likely implemented just like this internally:
void LookAt(Vector3 worldPosition, Vector3 worldUp = Vector3.up)
{
this.rotation = Quaternion.LookRotation(worldPosition - this.position, worldUp);
}
I really don't understand what you're actually asking. You talk about "same view" but it's unclear what you actually mean. You said cubeB is always not rotated so I assume cubeA can be rotated. However the whole relationship between the two cubes is not clear at all, at least to me.
Im sorry that my question is unclear. But i added two screenshots, i hope its now more understandable. CubeA dont rotate, but Cube B can be rotated.
Answer by BennyRemi · May 26, 2020 at 09:36 AM
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 :)
Answer by hexagonius · Apr 27, 2020 at 08:07 PM
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
The Camera is attached to the H$$anonymous$$D and i can fly around. So at any position of the camera, i can set Cube B in front of the camera, and Cube B should have the same perspective for the Camera as Cube A.