- Home /
The question is answered, right answer was accepted
geometric skybox camera?
This problem needs some explaining- what i mean by "geometric skybox", is that i have a sortof miniature version of scenery that i want to render around my level as if i built it full scale. i have 2 cameras, one to follow the player, and one to render the skybox layer geometry's "mini diorama". this works, it renders the diorama at the bigger scale. however, i want the skybox camera to move the way the player moves, but at the smaller scale, so that while the player is moving through the level, the skybox geometry appears to move correctly too.
to heavily simplify this complex issue:
i want object "B" to move when object "A" moves, and move with it, but at a smaller scale (aka, a smaller distance). how do i do this?
Answer by Bunny83 · Feb 26, 2018 at 02:34 AM
Well, the easiest, straight forward approach would be to:
create your 3d skybox geometry on a dedicated skybox layer.
Let the skybox camera only render that dedicated layer and exclude that layer from the main camera.
make sure your skybox camera is rendered before the main camera.
The main camera's clear flags should be set to "depth only".
Add a script to the skybox camera that syncs the movement with the main camera. Here you might specify the scaling factor that should be used for the camera syncing.
Note that it would be a good idea to create an empty gameobject where you add everything related to the skybox (the skybox geometry and the camera). So the empty gameobject would be the "world origin" for the skybox geometry. All your script has to do is this:
public Transform skyboxCam;
public Transform mainCam;
public float scale = 0.1f;
void Start()
{
if (mainCam == null)
mainCam = Camera.main.transform;
if (skyboxCam == null)
skyboxCam = GetComponent<Camera>();
}
void LateUpdate()
{
skyboxCam.localRotation = mainCam.rotation;
skyboxCam.localPosition = mainCam.position * scale;
}
Note that it's on purpose that we read the worldspace position and rotation from the main cam but setting the local space position and rotation of the skybox camera. This allows us to move / rotate our empty skybox root object whereever we want without changing anything.
$$anonymous$$eep in $$anonymous$$d that such a 3d skybox only makes sense on a restricted world. The "skybox geometry map" should have an empty area in the center (the actual area where the player will move). So make sure when the player reaches the border of your actual map he doesn't intersect with any skybox geometry.
Of course you can calculate the scaling factor based on the inverse which might be more convenient. Also since floating point numbers essentially work in base 2 using a power of two scaling factor would be a good idea ^^. The default scaling factor of the source engine is usually "16" (or "1/16" --> 0.0625)
i definetely know about source- i used to use source as my primary dev engine before realizing how antiquated it was and moving to unity after a few years. thats where i got the idea to use a 3d skybox; i have a level where I want it to be in a big city but the player can only go one linear path through it, and building tons of actual-sized buildings is too time consu$$anonymous$$g, so $$anonymous$$atures works better. :)
ps: Ins$$anonymous$$d of manually scaling the position you could simply scale the empty root gameobject for the skybox. This will have two effects in one: First everything inside the root will be scaled down. Also you can use the exact same coordinates as in the main world. So when assigning the position to the localPosition the skybox camera will automatically move 10 times slower when the scale is "0.1"
thank you! this worked very well; and i was even able to modify the script after i got it working, so that i can toggle a "debug mode" that traces the player's path through the level onto the scaled diorama, allowing me to see how i can structure it without getting weird issues with collision and stuff. :D
Follow this Question
Related Questions
raycast in direction of movement key down 2 Answers
Infinite random movement 0 Answers
How do I stop my character from sliding after adding force? 0 Answers
Player Control Script Makes Movement Buggy? 1 Answer
Camera Movement and angles 2 Answers