- Home /
Camera infinite rotating?
i have my code set so that the character looks around and it worked great! however as soon as i added the code to make the players y axis the same as the cameras it began to spin around infinetly, is there a better way to make the character track the y axis and stay even with it? using UnityEngine; using System.Collections; using System.Collections.Generic; [AddComponentMenu("Camera-Control/Smooth Mouse Look")] public class firstpersoncameracontrol : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public Transform player;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
void Update()
{
if (axes == RotationAxes.MouseXAndY)
{
float 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)
{
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);
}
player.transform.eulerAngles = new Vector3(0,transform.eulerAngles.y,0);
}
private void LateUpdate()
{
}
void Start()
{
Screen.lockCursor = true;
//if(!networkView.isMine)
//enabled = false;
// Make the rigid body not change rotation
//if (rigidbody)
//rigidbody.freezeRotation = true;
}
}
Answer by Bunny83 · Jul 24, 2018 at 11:05 PM
Do not read back the local euler angles and then increment / decrement the value. The eulerangles representation is not unique. Unity stores rotations as quaternions. When you read the euler angles they get converted from the quaternion. You should implement the x rotation just the same way you implement the y rotation. That is using a member variable of the script and just add the mouse motion to it.
It looks like you copied some premade script and just messed it up. At least the SmoothMouseLook on the wiki does have rotationX and rotationY variables. So your XandY case probably should look like this:
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationX = Mathf.Repeat(rotationX, 360f);
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
Of course as i said you want to define "rotationX" as a class member variable, right next to your rotationY variable.
Your answer
Follow this Question
Related Questions
First Person Controller Detect Texture 2 Answers
How do I make a working button? 1 Answer
how do i smooth between 2 clamps 1 Answer