I have this bit of code
using UnityEngine;
using System.Collections;
/// <summary>
/// Camera script. This script is attached to the player and causes
/// the camera to follow the Player's position and rotation.
/// </summary>
///
public class CameraScript : MonoBehaviour {
private Camera myCamera;
private Transform myTransform;
// Use this for initialization
void Start () {
myCamera = Camera.main;
myTransform = transform.FindChild("CameraHead");
}
// Update is called once per frame
void Update () {
myCamera.transform.position = myTransform.position;
myCamera.transform.rotation = myTransform.rotation;
}
}
which makes the camera follow the player’s position and rotation like a FPS. But what I need is for the Camera so only follow the players X rotation while I have the mouse control the cameras Y rotation. I’ve tried doing this.
myCamera.transform.rotation.x = myTransform.rotation.x;
but that throws up errors. Any idea on how I can separate the X and Y rotation in this case?
This should get you startet. The script is attached to the camera which is a child of your player object.
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
void Update ()
{
if(Screen.lockCursor == true) {
if (axes == RotationAxes.MouseXAndY) {
float rotationX = new float();
if(transform.parent != null && transform.parent.CompareTag("Player") == true) {
rotationX = transform.parent.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.parent.transform.localEulerAngles = new Vector3(0, rotationX, 0);
transform.localEulerAngles = new Vector3(-rotationY, 0, 0);
}
else if(transform.parent != null) {
rotationX = transform.parent.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.parent.transform.localEulerAngles = new Vector3(0, rotationX, 0);
transform.localEulerAngles = new Vector3(-rotationY, 0, 0);
}
/*
else {
rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
*/
}
else if (axes == RotationAxes.MouseX) {
if(transform.parent != null) {
transform.parent.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else {
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
}
else {
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
}