Help with my almost correct follow player camera script.

Hi all,

I have put together this camera script, which I want to follow the PLAYER as well as orbit the PLAYER through via mouse look input. This is all working except for the slight problem that the X mouse movements don’t properly rotate. The Y is better but both seem to have a sort of limit of closeness to the PLAYER at which point they start to move in the opposite direction.

I’d like the camera movement controlled by the mouse and to be able to rotate completely around the PLAYER horizontally and go in real close vertically.

Any help would be awesome guys!

using UnityEngine;
using System.Collections;

public class RotateCamera : MonoBehaviour {
    public float turnSpeed = 4.0f;
    public Transform player;

//     The target of which to follow (player in this case)
    public Vector3 CameraOffset = new Vector3(0,20,-10); // This will allow us to offset the camera for the player's view.



    public float height = 1f;
    public float distance = 2f;
  
    private Vector3 offsetX;
    private Vector3 offsetY;
  
    void Start () {
  

        offsetX = new Vector3 (0, height, distance);
        offsetY = new Vector3 (0, 0, distance);
    }
  
    void LateUpdate()
    {




        Vector3 offset = CameraOffset;
  


        //Debug.Log (distanceFromPlayer);
  
        offsetX = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offsetX;
        offsetY = Quaternion.AngleAxis (Input.GetAxis("Mouse Y") * turnSpeed, Vector3.right) * offsetY;



            transform.position = player.position + offsetX + offsetY + CameraOffset;
            transform.LookAt(player.position);

      
    }
}

So here is the progress I’ve made:

Can now rotate completely around player on horizontal rotation. Still have an issue with the vertical rotation and have been trying to put in constraints. I think they are working but when playing the game the camera has constant movement on the vertical axis, but within those constraints.

I only want movement from player input, but cannot see the problem in the script. I’m pretty new to scripting and would really appreciate the help.

Many thanks.

using UnityEngine;
using System.Collections;

public class RotateCamera : MonoBehaviour {
    public float turnSpeed = 4.0f;
    public Transform player;

//     The target of which to follow (player in this case)
    public Vector3 CameraOffset = new Vector3(0,1,0); // This will allow us to offset the camera for the player's view.
    public float yConstraint = 60f;
    public float rotationY = 0;


    public float height = 1f;
    public float distance = 2f;
   
    private Vector3 offsetX;
    private Vector3 offsetY;

    private Camera _camera;



    void Start () {
   
        _camera = GetComponent<Camera>();

        offsetX = new Vector3 (0, height, distance);
        offsetY = new Vector3 (0, 0, distance);

    }
   
    void LateUpdate()
    {
       
//        player coordinates
        Vector3 targetPos = player.transform.position;

//        Get Y axis
        rotationY -= Input.GetAxis("RS_Vertical");

//        Constraint Yaxis
        rotationY = Mathf.Clamp (rotationY, -yConstraint, yConstraint);
   
//        work out offsetX
        offsetX = Quaternion.AngleAxis (Input.GetAxis("RS_Horizontal") * turnSpeed, Vector3.up) * offsetX;

//        work out offsetY
        offsetY = Quaternion.AngleAxis (rotationY * turnSpeed, Vector3.right) * offsetY;


//        move camera
        _camera.transform.position = targetPos + offsetX + offsetY + CameraOffset;

//        always looking at player
        transform.LookAt(targetPos);
   

       
    }
}