How do I make a rotation around the player script for the camera?

I was trying to make it so the player would have control over rotating around the fps player (sphere) but the rigidbody Freeze rotation isn’t on so the sphere can have a nice looking rolling look while moving. The camera would supposedly be limited to certain vertical degrees and when pressing W the player would move forward.

Here is my script:

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float speed;

    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

        rb.AddForce(movement * speed);
    }
}

Also in case you need it I’ll leave the rotation camera script here as well, in which I have failed:

using UnityEngine;

public class CameraRotate : MonoBehaviour
{

    public GameObject target;
    float speed = 5f;
    float minFov = 35f;
    float maxFov = 100f;
    float sensitivity = 17f;

    void Update()
    {
        if (Input.GetMouseButton(1))
        {
            transform.RotateAround(target.transform.position, transform.up, Input.GetAxis("Mouse X") * speed);
            transform.RotateAround(target.transform.position, transform.right, Input.GetAxis("Mouse Y") * -speed);
        }
        float fov = Camera.main.fieldOfView;
        fov += Input.GetAxis("Mouse ScrollWheel") * -sensitivity;
        fov = Mathf.Clamp(fov, minFov, maxFov);
        Camera.main.fieldOfView = fov;
    }
}

Camera stuff is pretty tricky… it might be best to use Cinemachine from the Unity Package Manager.

1 Like

Yesss, I was going crazy with this issue, the cinemachine made it incredibly simple to have some decent 3rd person movement. Thank you and have a great day!

1 Like