Tuesday, August 25, 2015

Unity3D At Home Project - Day 3 - Basic Camera Controller

So I finished off Renaissance Coder's character controller tutorial (you can find it here) with implementing a basic camera controller.  And by basic, I mean really basic. You give it a target, and it slaves its position to the target, plus an offset that you give it. For orientation, it just interpolates from its current orientation to that of the target. Even though it was a fairly small piece of code to write, I still learned a few interesting things, and had some hiccups.

Cool Things

Mathf.SmoothDampAngle
This does rotational interpolation for you. You give it a source value in radians, a destination radians, and some time values that determine how fast you want the function to interpolate towards your destination. It returns an updated angle, and also modifieds a turn velocity float that you can use in successive updates.  Really handy function.

LateUpdate
I'd forgotten about LateUpdate, but this is an update function that occurs each frame after normal Update and all of the FixedUpdates. By updating the camera position and rotation in this function, we ensure it happens after the other updates.

Hiccups

My initial offset for the camera position had the sign of the Y offset reversed, which placed the camera somewhat inconveniently underground. While cool and interesting, wasn't exactly the effect we were going for.







The other thing I noticed immediately after my first test was that the camera was still facing straight ahead - not actually looking at the player. The tutorial had mentioned an X-tilt factor, but had failed to ever actually hook it up and use it. I converted the value to radians, applied the rotation to the target's X rotation (which is always straight ahead), and then used the editor to adjust the value until  we got to something close. What was interesting was that when I changed display resolutions, the amount of avatar in the camera field of view changed. Which goes to show you the kind of BS you actually have to deal with when building a real camera.

You can see the functional camera below.



Notice my gravity quotient is way way too low, so when I go sailing off the tops of hills, I only sort of gently float back to ground over time. Also notice even the leaves of the tree have collision, lol.

What next?
Well I'm not 100% sure. The camera controller is functional, but only barely. There's no orbit control, and there's no collision. But building a robust camera controller can be a pretty dark rabbit hole - correclty and intuitively handling camera collision is a non trivial task.  So I could leave the controller alone, and move on to animation. But by the same token, the current camera is by no means near adequate, so we'll have to see.

No comments:

Post a Comment