Camera

The Camera Controller component is responsible for moving the camera to the correct rotation and position. When the character is attached to a camera (such as for a player-controlled, non AI character) then the Camera Controller is also responsible for determining which direction the character should look or use their item. As a result of this the Camera Controller is required for any player-controlled character.

The Camera Controller component doesn’t determine the actual position or rotation that the camera should move towards. It instead uses View Types to determine those values for it. View types can be thought of as Abilities but for the camera. View types allow for complete flexibility in terms of how the camera behaves without having to change the core Camera Controller component. View types can also be used to integrate with any external camera controllers, such as Cinemachine.

When the Camera Controller starts it will first try to attach to the character specified by the Character field if Init Character On Awake is enabled. If this option is enabled but there isn’t a character specified then it’ll show a warning and search for the GameObject that has the Player tag. If there still is no character found then the Camera Controller will disable itself until a Character is assigned.

While the Camera Controller does provide the information so the character knows where to look/use an item, the Camera Controller should not be used for artificial intelligence (AI) or networked characters. See the corresponding AI and networking pages for information on how these characters should be setup.

API

When the camera initializes it will either assign the character automatically based on the Player tag or use the character set within the inspector. If you’d like to change which character the camera follows you can set the Character property. If this value is set to null then the camera will disable itself. The camera can also change perspective with the SetPerspective method if the camera has a first and third person view type added to it. View types can be changed with the SetViewType method.

using UnityEngine;
using Opsive.Shared.Camera;
using Opsive.Shared.Utility;
using Opsive.UltimateCharacterController.Camera;

public class MyObject : MonoBehaviour
{
    [Tooltip("The character that should be assigned to the camera.")]
    protected GameObject m_Character;

    /// <summary>
    /// Sets a third person perspective on the Camera Controller.
    /// </summary>
    private void Start()
    {
        var camera = CameraUtility.FindCamera(null);
        if (camera == null) {
            return;
        }

        var cameraController = camera.GetComponent<CameraController>();
        cameraController.Character = m_Character;
        cameraController.SetPerspective(false); // false indicates the third person perspective.

        // Switch to the third person Combat View Type.
        var viewTypeName = "Opsive.UltimateCharacterController.ThirdPersonController.Camera.ViewTypes.Combat";
        cameraController.SetViewType(TypeUtility.GetType(viewTypeName), false);
    }
}

Events

The Camera Controller exposes two events using the built-in Event System: OnChangeViewTypes and OnChangePerspectives. Corresponding Unity events are also exposed for these two events.

OnChangeViewTypes

The “OnCameraChangeViewTypes” event will be execute when the Camera Controller activates or deactivates a view type. This event can be subscribed to with:

EventHandler.RegisterEvent<ViewType, float>(gameObject, "OnCameraChangeViewTypes", OnChangeViewTypes);

The gameObject variable in this example is referring to the Camera’s GameObject. This event includes the view type that was changed as well as if the view type was activated or deactivated.

/// <summary>
/// The view type has changed.
/// </summary>
/// <param name="viewType">The ViewType that was activated or deactivated.</param>
/// <param name="activate">Should the current view type be activated?</param>
private void OnChangeViewType(ViewType viewType, bool activate)
{
        Debug.Log("The ViewType " + viewType.GetType().Name + " was " + (activate ? "activated" : "deactivated") + ".");
}
OnChangePerspectives

The “OnCameraChangePerspectives” event will be executed when the Camera Controller switches from a first to third perspective or from a third to first perspective. This event can be subscribed with:

EventHandler.RegisterEvent<bool>(gameObject, "OnCameraChangePerspectives", OnChangePerspectives);

The gameObject variable in this example is referring to the Camera’s GameObject. The only parameter sent with this event includes a bool indicating if the camera is now in a first person perspective (true) or a third person perspective (false).

/// <summary>
/// The camera perspective between first and third person has changed.
/// </summary>
/// <param name="firstPersonPerspective">Is the camera in a first person perspective?</param>
private void OnChangePerspectives(bool firstPersonPerspective)
{
        Debug.Log("The camera is now in a " + (firstPersonPerspective ? "first" : "third") + " person perspective.");
}
OnZoom

The “OnCameraZoom” event will be executed when the Camera Controller starts or stop zooming. This event can be subscribed to with:

EventHandler.RegisterEvent<bool>(gameObject, "OnCameraZoom", OnZoom);

The gameObject variable in this example is referring to the Camera’s GameObject. The only parameter sent with this event includes a bool indicating if the camera is zoomed (true) or not zoomed (false).

/// <summary>
/// The camera has toggled zoom state.
/// </summary>
/// <param name="zoomed">Is the camera zoomed?</param>
private void OnZoom(bool zoomed)
{
        Debug.Log("The camera is now in a " + (zoomed ? "zoomed" : "not zoomed") + " state.");
}

Inspected Fields

Init Character On Awake

Should the character be initialized on awake?

Character

The character that the camera should follow.

Anchor

The transform of the object to attach the camera relative to. This for example allows the camera to be attached to the character’s head instead of the base pivot position. Note that if the camera is attached to a body part that moves there will be a lot more motion when the character moves.

Auto Anchor

Should the anchor be assigned automatically based on the humanoid bone?

Auto Anchor Bone

The bone that the anchor will be assigned to if AutoAnchor is enabled.

Anchor Offset

The offset between the anchor and the camera.

First Person View Type

The active first person ViewType. This will only be shown if both the First Person Controller and Third Person Controller is imported.

Third Person View Type

The active third person ViewType. This will only be shown if both the First Person Controller and Third Person Controller is imported.

Can Change Perspectives

Can the camera change perspectives?

Can Zoom

Can the camera zoom?

Zoom State

The state that should be activated when zoomed.