Controlling Unity Camera Behavior - 2019.3

Tutorial

·

intermediate

·

+0XP

·

25 mins

·

Unity Technologies

Controlling Unity Camera Behavior - 2019.3

In this tutorial, we’ll dive deeper into Unity Cameras with scripts that control their behavior. We’ll start out with a 2D Camera that works from any perspective (in this case, we’ll be using it in an overhead view). Next, we’ll move on to 3D with a Camera that smoothly transitions — with a flick of the mouse scroll wheel — from first-person to third-person and back. This Camera is suitable for games, VR, or architectural walkthroughs, as seen in many modern and classic 3D titles.

Languages available:

1. Introduction

If you are using Unity 2019.2 or below, click here.


In this tutorial, we’ll dive deeper into Unity Cameras with scripts that control their behavior. We’ll start out with a 2D Camera that works from any perspective (in this case, we’ll be using it in an overhead view). Next, we’ll move on to 3D with a Camera that smoothly transitions — with a flick of the mouse scroll wheel — from first-person to third-person and back. This Camera is suitable for games, VR, or architectural walkthroughs, as seen in many modern and classic 3D titles.


In most cases, rather than remain static, you want Unity’s Camera to follow or track your player or avatar. In Unity, to tie the position of object A (Camera) to object B (player or avatar), a common tactic is to set object A as a child of the controlling object B. The position, scale, and rotation of A are then directly influenced by B. In practice, this isn’t usually a good approach for games or walkthroughs. Because the player or avatar can move, stop, and turn quickly, parenting it to the Camera would result in a very uncomfortable experience for the user. Instead, it’s better to have an object track the player, and the camera, in turn, track that object. This keeps the player in view, but smooths Camera motion.


2. Smooth 2D Camera Motion

In this section, we’ll build a simple system for tracking the player. This works with any perspective in 2D. We include a simple player movement script for an overhead perspective game like The Legend of Zelda from Nintendo.


1. Create an empty object by selecting Create Empty from the GameObject drop-down. Name it Player. Set its position to 0 on the Z axis.


2. Add a Sprite Renderer component and set the Sprite to your player Sprite. If you don’t have one, you can right-click in the Project view and select Create > Sprites > Square.


3. Add a new C# script to Player, named PlayerMovement.


4. Double-click PlayerMovement in the Project view to open the script editor.


5. Beginning on line 7, type:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

6. Delete Start, as we won’t be using it.


7. In Update, type:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

Below is the completed PlayerMovement script:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

8. Save changes and return to the Unity Editor.


9. Create another empty GameObject, named TrackerObject. Set its position to 0 on the Z axis.


10. Attach a new C# script component, called Tracker, to TrackerObject.


11. Double-click Tracker to open the script in your editor.


12. Beginning on line 7, type:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

13. In Start, type:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

14. This allows the Camera to maintain its distance from the Scene when tracking Scene objects.


15. Change Update to LateUpdate. This makes sure the player is done moving for this frame before the tracker moves toward its position. In LateUpdate, type:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

Below is the completed Tracker script:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

16. Save changes and return to the Unity Editor.


17. Select TrackerObject.


18. In the Tracker Inspector, drag the Player into the slot marked Tracked Object (Figure 01).


Figure 01: Tracker settings

Figure 01: Tracker settings


19. Set Update Speed to 7.


20. Attach the Tracker component to the Main Camera.


21. Set TrackerObject as the Tracked Object, and set Update Speed to 10.


22. Enter Play mode. Move the player using the arrow keys, WASD, or a controller. Notice that the Camera now smoothly tracks the player.


3. Smooth 3D Camera Motion

In this section, we’ll build a Camera for 3D Scenes that can be either first- or third-person, with a controllable distance behind the avatar. The distance is controllable via the mouse scroll wheel, and in the Unity Editor, via a slider in the Inspector. When the Camera gets within a specified distance, the player disappears, allowing for first-person views. This time the Camera will track the player directly, rather than an intermediate object.


1. If you already have a 3D player set up in your Scene, skip to step 5. If not, create a 3D cube by selecting 3D Object > Cube from the GameObject drop-down. Name it Ground.


2. Scale this in the X and Z axes to create enough ground for your player to cover (Figure 02).


Figure 02: Transform settings for the ground

Figure 02: Transform settings for the ground


3. Create a capsule by selecting 3D Object > Capsule from the GameObject drop-down. Name it Player. Set its position to 0 on the Z axis.


4. Attach a Rigidbody component to Player, and set the constraints to lock rotation on the X and Z axes to prevent the Player from falling over (Figure 03).


Figure 03: Constraining rotation on the X and Z axes prevent our character from falling over, while still allowing them to turn.

Figure 03: Constraining rotation on the X and Z axes prevent our character from falling over, while still allowing them to turn.


5. Add a new C# script to Player, named PlayerMovement3D.


6. Double-click PlayerMovement3D in the Project view to open the script editor.


7. Beginning on line 7, type:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

8. In Start, type:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

9. In Update, type:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

Below is the completed PlayerMovement3D script:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

10. Save changes and return to the Unity Editor.


11. Add a new C# script to the Main Camera, called PlayerTracker.


12. Double-click PlayerTracker to open the script editor.


13. Beginning on line 7, type:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

14. In Start, type:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

15. Change Update to LateUpdate. In LateUpdate, type:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

Below is the completed PlayerTracker script:


[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

16. Save changes and return to the Unity Editor.


17. In the slot labeled Tracked Object in PlayerTracker, add the Player object (Figure 04).


Figure 04: Configuring the Player Tracker.

Figure 04: Configuring the Player Tracker.


18. Enter Play mode. Move the player using the arrow keys, WASD, or a controller. Try moving the Camera toward and away from the player by using the mouse scroll wheel, or, if using a touchpad, the virtual scroll.


19. Notice that the Camera smoothly tracks the player, hiding the player when it gets too close to prevent obstruction of view.


20. Exit Play mode.


4. Conclusion

The Camera is the window into your project’s world, and its behavior is as important as that of any character to ensure a positive player experience.


Complete this tutorial