How can I make the camera to orbit also up and down?

Viewed 43

Should I use Cinemachine for that or it's better to do it with a script from scratch ?

The script is attached to the Main Camera.

Now it's orbit only left right. I want it to orbit 360 degrees with clamp so it wont move down to the floor and to up back.

I should also the Y and not only the X but not sure how.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerFollow : MonoBehaviour {

    public Transform PlayerTransform;

    private Vector3 _cameraOffset;

    [Range(0.01f, 1.0f)]
    public float SmoothFactor = 0.5f;

    public bool LookAtPlayer = false;

    public bool RotateAroundPlayer = true;

    public float RotationsSpeed = 5.0f;

    // Use this for initialization
    void Start () {
        _cameraOffset = transform.position - PlayerTransform.position;  
    }
    
    // LateUpdate is called after Update methods
    void LateUpdate () {

        if(RotateAroundPlayer)
        {
            Quaternion camTurnAngle =
                Quaternion.AngleAxis(Input.GetAxis("Mouse X") * RotationsSpeed, Vector3.up);

            _cameraOffset = camTurnAngle * _cameraOffset;
        }

        Vector3 newPos = PlayerTransform.position + _cameraOffset;

        transform.position = Vector3.Slerp(transform.position, newPos, SmoothFactor);

        if (LookAtPlayer || RotateAroundPlayer)
            transform.LookAt(PlayerTransform);
    }
}
0 Answers
Related