- Home /
How can I make player rotation control camera's x rotation and mouse control it's y rotation?
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?
Just to make sure I've visualizing correctly, you want the camera to follow the player as the player is looking up or down. Correct?
Answer by Cherno · May 17, 2014 at 01:02 PM
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);
}
}
}
Thank you this is much much closer to what I want than the standard mouselook script, but there are still a couple problems I'm facing. $$anonymous$$y player is a rigidbody and with this script attached to the camera and my camera a child of the player. When I walk up ramps and stuff I get pulled back down and then the player continues to travel uncontrollably in the direction they fell down the ramp. This did not occur with the original mouselook script. Also, the biggest problem I am trying to solve is that my game has 6 players and each player (rigidbody) has it's own gravitational direction. So I have one player on the ceiling one player on the floor, one player and each wall. I have a script that rotates each player so they are facing upright on their particular wall and gravity applied in the proper direction for each player. Well when using $$anonymous$$ouseXandY with either mouselook scripts the player is turned back facing the global upright position as if every player stood on the floor. I am pretty sure this has to do with the Y Clamp and this line transform.parent.transform.localEulerAngles = new Vector3(0, rotationX, 0);, but I don't know how to modify it for my purposes. Any ideas?