Hey all.
I am working on a puzzle game where the camera rotates around a lot. The camera rotates when starting a level, to show the puzzle, and the player can rotate the camera when they are playing.
I have 3 menus, the in-game menu, the win menu, and the lose menu, that I instantiate at the start of the game, and I hide and show them when necessary.
My issue is that, depending on how the camera is rotated when the player plays, the menus show up differently. I instantiate them all at the start of the game, and parent them to the camera, and then I move them back and forth from that position to an off-camera position when I want to show and hide them. I thought this would eliminate this issue, but depending on what is being done in the level, the menus show up in weird places, and at odd angles.
My current thought is that I could take the camera’s position, and set the menu equal to the camera’s position, with a little adjustment to make the menu right in front of the camera. But I’m not sure how to do this. I basically need to get a normalized vector that points straight from where the camera points, and add it to the camera position to get the new menu position. Is this possible?
If your menus are child objects of the camera make sure that your script that moves them into view is working in local space (transform.localPosition instead of transform.position, etc…).
I would suggest creating another camera that handles your GUI. The camera can either exist in a part of the scene that will never be seen by the player camera or it can be set to only show the layers that your GUI resides in. You can see an example here as shown in Smuggle Truck:
Just make sure your GUI camera has a higher depth and the clear flags are set to “Depth only”.
Oh and to do what you described (though I wouldn’t recommend actually using this method unless there is a specific need to) you would do something like this:
var menuPrefab:GameObject;
var playerCamera:Camera;
public function instantiateMenu() {
// This assumes that when the menu is set to the same position / rotation of the playerCamera the menu is facing the camera
var menuGO:GameObject = Instantiate(menuPrefab, playerCamera.transform.position, playerCamera.transform.rotation);
menuGO.transform.parent = playerCamera.transform;
// Move the menu object in local space so it is in front of the camera (adjust the distance as necessary)
menuGO.transform.localPosition.z -= 1;
}
Thanks, those are really great details! Much appreciated.
I actually figured out a way myself:
transform.position = transform.parent.transform.position + transform.parent.transform.forward;
Where parent is the main camera. That puts the menu right in front of the camera, and it works great for all my menus.