Simple Third Person Camera With First Person Zoom
A downloadable tool
Happy to present my first paid asset: a simple third person camera follow script with first person zoom. The third person camera is pretty solid while the first person zoom was added purely for the cosmetic function of looking closely at the environment, but if you've got some C# knowledge, it could easily be modified into an aim feature or other interesting mechanic.
Quick Summary:
- Simple setup: Attach the PlayerController and CameraController components to your Player object, configure them as desired and start running around. Note that a CharacterController is also required by PlayerController (CharacterController and CameraController should be added automatically when applying PlayerController).
- Third Person camera smoothly follows the character from some zoom distance. By default, the character is able to run toward the camera, but setting cameraStaysBehindCharacter to true under CameraController options will ensure the camera is always at the character's back.
- Zoom in and out with the mouse wheel.
- Rotate the camera by holding right click.
- Speed and limits for zoom and rotation can be configured under CameraController options.
- UnityEngine.EventSystems is used by CameraController to detect if the player is hovering over UI, and if so, the mouse functions to zoom/rotate will not activate.
- If allowFirstPersonTransition is enabled, zooming within a certain configurable distance of the character will transition the camera view to First Person Mode. Zooming out will return to Third Person Mode.
- First Person Mode has additional configurable options on PlayerController; setting useStrafeInFirstPerson to true will move the character sideways without changing their facing direction, as opposed to applying a smooth rotation in that direction.
- LayerMask cameraCollisionMask under the CameraController options can be configured to ensure the camera will 'collide' with objects on these layers, moving the camera forward toward the player if needed. You'll probably want this to reference the ground and large scene objects to avoid looking through them.
- CameraController provides a ShakeCamera() function which can be accessed by calling CameraController.ShakeCamera(duration, intensity). Values of 0.5f and 1.5f, showcased by PlayerController's debug functions, create a modest, brief shake.
Limitations:
- PlayerController script is a barebones movement script meant to serve as an example or starting point for coding your own movement which will play as intended alongside CameraController, meaning it is assumed you have some coding knowledge to implement features you might desire, such as to jump or use alternate input methods. I've done my best to document the functions to make editing/creating your own movement script as simple as possible.
- a lookAtOverride object can be set to alter the camera's positioning behavior relative to the character, but changing this object reference during runtime is not supported.
- By default, if no camera is assigned for use by CameraController, the script will try to find and use the main camera in the scene. Changing this reference at runtime is not supported.
- The transition from third person to first person can sometimes feel a little awkward - if the player is facing the camera, for example, and they zoom in to first person view, the camera will flip around to the direction the player is facing rather than turning the player toward the direction the third person camera was facing. I would consider improving this in future updates. Remember the first person transition can be disabled altogether if not desired.
I like to provide free assets as much as possible, but I spent so much time and headache on this one that I simply must ask a little something in return for time and skill invested. This camera code is what I personally use in my own third person projects and I've found it to work smoothly so far, but I will admit there could be bugs I've yet to encounter. If you run into problems, you can reach me at milkshakebattlecat@gmail.com
If purchasing this asset, bear in mind: Unity's own Cinemachine asset is a fine free solution that can do everything these scripts can do and more. I simply desired an alternative that didn't have me messing around with camera rigs and seeking out other unfamiliar functions to achieve certain behaviors I desired. It's my intention to keep this camera asset as lightweight as possible, though if there is interest, I may be persuaded to update the tool with a wider range of PlayerController scripts that showcase different movement styles and input options for different project scenarios. In fact, as I explain how these scripts work in tandem, it occurs to me that I might could define some of the movement calculations in the CameraController script itself to make it even more user-friendly to write your own custom PlayerController script. I'll think about that for future updates.
Some tips:
- If you have the problem of the camera being positioned too low to the ground when you zoom out of first person mode, try setting a lookAtOverride object positioned at the character's head or chest.
- A lookAtOverride object positioned a small distance in front of the player's head could also eliminate the need for having to write the Show/Hide Renderers placeholder functions in CameraController.cs, as the camera would simply stay in front of the model in that case. You might still end up seeing animations from sword swings and the like, though.
- Relevant functions of PlayerController include: GetCameraRelativeDirection() which ensures the player always moves "forward" based on the direction the camera is facing, while GetFirstPersonMovement() contains logic which calculates forward/backward direction based on the player's current facing direction when in the first person view mode. These showcase the functionality you will probably want for making your own custom movement script.
Hope it helps. Good luck making your games!
-
Background Image by: [freepik]
Published | 2 days ago |
Status | Released |
Category | Tool |
Author | MILKSHAKEBATTLECAT |
Tags | camera, first-person-freelook, third-person-camera, Unity |
Purchase
In order to download this tool you must purchase it at or above the minimum price of $5 USD. You will get access to the following files:
Leave a comment
Log in with itch.io to leave a comment.