Level Builder - From Pixels to Playable Level Minute 5 to Minute 10 - Completing the Game Loop Then inside the Zoom Camera function, we’ll cache a reference to the y component of the scroll wheel input and divide by 100 - this scales the value to something more useful (in my opinion). We also need to set the value of zoom height equal to the local y position of the camera - this gives an initial value and prevents the camera from doing wacky things. Just like we did with the rotation we need to subscribe and unsubscribe to the performed event for the zoom camera action. This zoom motion is another good use of events so we need need to make a couple of additions to the OnEnable and OnDisable. I wanted there to be a min and max height for the camera - this keeps the player from zooming too far out or zooming down to nothingness - also while going up and down it feels a bit more natural if the camera gets closer or farther away from what it’s looking at. This motion, I found to be one of the more complicated as there were several bits I wanted to include. For controlling the “zooming” we’ll be using the mouse scroll wheel. With horizontal and rotational motion working it would be nice to move the camera up and down to let the player see more or less of the world. Our last step before we can test our code is to add our last three functions into the update function. The final step is to set the target position to zero to reset for the next frame’s input. With the horizontal velocity lerping it’s way towards zero, we then add to the transform’s position a value equal to the horizontal velocity multiplied by time delta time. Note rather than using our acceleration to control the rate of the slow down, I’ve used a different variable (damping) to allow separate control. To do this we want to lerp our horizontal velocity (calculated constantly by the previous function) down to zero. If the player is not trying to get the camera to move we want the camera to smoothly come to a stop. While they might look different these two lines of code are closely related to the Kinematic equations you may have learned in high school physics. We then add to the transform’s position an amount equal to the target position multiplied by the current camera speed and time delta time. We then normalize the resulting vector to keep a uniform length so that the speed will be constant even if multiple keys are pressed (up and right for example). This ensures that the movement is in the horizontal plane and relative to the camera. We’ll take the x component of the input and multiply it by the Camera Right function and add that to the y component of the input multiplied by the Camera Forward function. In order to translate the input into the motion we want we need to be a bit careful. This function polls the Camera Movement action to then set the target position. The next function is the poorly named Get Keyboard Movement function. This ensures we are calculating the velocity for the frame and not from the start. After calculating the velocity we update the value of the last position for the next frame. Nothing too special in the function other than once again squashing the y dimension of the velocity to zero. It feels a bit clumsy, but since I’m not using a rigidbody and I want the camera to smoothly speed up and slow down I need a way to calculate and track the velocity (in the horizontal plane). Admittedly I don’t love the next function.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |