Possible variation on Camera

I’m still working on whether I want a truly fixed camera. I know I want it at a fixed distance, but depending on mechanics I decide on, I may move from a click-to-move to using an orbit camera and WASD movement. With the default third person controller, this also gives the costless bonus of being able to control turning and doing “quick” turns using the orbit.

I’m not using the method right now, but I wanted to share the additions to my camera script for anyone else who might be interested:

using UnityEngine;

public class FollowCamera : MonoBehaviour
{
    public float OrbitSensitivity = 5f;
    public float PitchSensitivity = 5f;
    GameObject _Player;

    float x = 0f;
    float y = 0f;
    float yMin = -45f;
    float yMax = 40f;

    private void Awake()
    {
       // Uncomment for orbit camera use
       // Cursor.lockState = CursorLockMode.Locked;
       // Cursor.visible = false;

        _Player = GameObject.FindGameObjectWithTag("Player");
    }

    private void LateUpdate()
    {
        DoPositionUpdate();
    }

    void DoPositionUpdate()
    {
        transform.position = _Player.transform.position;
    }

    void DoOrbit()
    {
        x += Input.GetAxis("Mouse X") * OrbitSensitivity;
        y -= Input.GetAxis("Mouse Y") * PitchSensitivity;

        y = ClampAngle(y, yMin, yMax);

        Quaternion rotation = Quaternion.Euler(-y, x, 0);

        transform.rotation = rotation;
    }

    float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360F)
            angle += 360F;
        if (angle > 360F)
            angle -= 360F;
        return Mathf.Clamp(angle, min, max);
    }
}

EDIT: Note that I haven’t added collision detection for walls. Right now the camera just passes through them. I’ll add a distance component to the method later.

1 Like

so in this case I can move my visual rotation with W A S D but the player still in the same position in the screen, right ?

Privacy & Terms