I’ve tried all day to get this working and I can’t. I’m trying to get a camera to bind to the mouse that rotates around the player, and the camera will follow them.
The same script will also include bindings for a right analog stick on a Xbox Controller…
using UnityEngine;
using System.Collections;
public class CameraPlayer : MonoBehaviour
{
public Transform lookAt, camTransform;
private Camera cam;
private float distance = 5.0f;
private float currentX = 1.0f;
private float currentY = 1.0f;
private float sensitivityX = 0.0f;
private float sensitivityY = 0.0f;
private const float Y_ANGLE_Min = 0.0f;
private const float Y_ANGLE_Max = 50.0f;
private void start()
{
camTransform = transform;
cam = Camera.main;
}
void Update()
{
//Debug.Log("Update called");
currentX += Input.GetAxis("Mouse X");
currentY += Input.GetAxis("Mouse Y");
//currentX += Input.GetAxis("Horizontal");
//currentY += Input.GetAxis("Vertical");
currentX += Input.GetAxis("Joy X");
currentY += Input.GetAxis("Joy Y");
currentY = Mathf.Clamp(currentY, Y_ANGLE_Min, Y_ANGLE_Max);
}
private void LateUpdate()
{
Vector3 dir = new Vector3(0, 0, -distance);
Quaternion rotation = Quaternion.Euler(currentX, currentY, 0);
camTransform.position = lookAt.position + rotation * dir;
camTransform.LookAt(lookAt.position);
}
}