Toggling between 1st and 3rd person views

In the Camera Controller videos and documentation, I show how to use the Camera Controller to toggle between motors. While I show how to create a ‘targeting view’, the same process applies to swapping between the 1st person and 3rd person views…

We are just transitioning between motors.

 

Steps

1. Add a 1st Person Motor
2. Add a 3rd Person Motor
3. Add a transition motor from 1st person to 3rd person
4. Add a transition motor from 3rd person to 1st person

You’ll set the 1st person and 3rd person motors up however you’d like.

5. Add Unity Input Manager entry

Since we want a toggle (press to go to 3rd person view and press again to go back to 1st person), we’ll setup both transitions with the same input alias. So, we need to add an entry to Unity’s Input Manager.

Here I added an entry called “Camera View Change” and associated it with the F8 key.

 

Transitions

Now, we can setup the transitions using the approach I talk about in the documentation:

 

Motor Activated Events

This part really depends on your game design. If you want to make other adjustments, you can react to the camera motor changes.

For example, let’s say you want your character to walk with the Basic Walk Run Strafe motion when in 1st person, but use Basic Walk Run Pivot when in 3rd person. You can make those changes through the MotorActivated event.

First, create the MonoBehaviour that will make your changes. This would be an example:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using UnityEngine;
using com.ootii.Actors.AnimationControllers;
using com.ootii.Cameras;
using com.ootii.Messages;

public class ChangeView : MonoBehaviour
{
    public void OnCameraMotorActivated(IMessage rMessage)
    {
        CameraMessage lMessage = rMessage as CameraMessage;
        if (lMessage == null) { return; }

        // Change the MC motion based on the 1st person view that is now active
        if (lMessage.Motor is ViewMotor)
        {
            MotionController lMC = gameObject.GetComponent<MotionController>();

            MotionControllerMotion lStrafeMotion = lMC.GetMotion("BasicWalkRunStrafe", true);
            lStrafeMotion.IsEnabled = true;

            MotionControllerMotion lPivotMotion = lMC.GetMotion("BasicWalkRunPivot", true);
            lPivotMotion.IsEnabled = false;
            lPivotMotion.Deactivate();
        }
        // Change the MC motion based on the 3rd person view that is now active
        else if (lMessage.Motor is OrbitFixedMotor)
        {
            MotionController lMC = gameObject.GetComponent<MotionController>();

            MotionControllerMotion lPivotMotion = lMC.GetMotion("BasicWalkRunPivot", true);
            lPivotMotion.IsEnabled = true;

            MotionControllerMotion lStrafeMotion = lMC.GetMotion("BasicWalkRunStrafe", true);
            lStrafeMotion.IsEnabled = false;
            lStrafeMotion.Deactivate();
        }
    }
}

The code may look a bit scary, but all we’re doing is checking the new motor type. Then, we grab the Motion Controller and set the walk/run motions’ enabled flag as we described.

Change the code as needed and attach this component to your Motion Controller character.

Finally, set this event handler on the Camera Controller’s Motor Activated Event.

 

Summary

Using the transition motors, you can now transition between two different motors. In this case, 1st person and 3rd person views.

The Motor Activated event allows us to add game specific logic when the motors change. In this case, we’re changing the walk style too.

 

Leave a Reply