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);
}
}