I am trying to modify the given MouseLook.cs script to be used with rotations on the x and z axis rather than the x and y axis. I am working on a maze game in which you move the maze itself to role a ball through the maze and this rotation system with sensitivity, min and max rotations, will work perfect for that if I can change the y rotation to a z rotation but this is proving more difficult for me than expected. here is the code:
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
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;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Horizontal") * sensitivityX;
rotationY += Input.GetAxis("Vertical") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY,rotationX,0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Horizontal") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Vertical") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
}
At this point the only only modifications I have done to the mouse look code is to switch to using vertical and horizontal values. The parts related to the x rotation work as I want them to, So I need to try to replicate that idea onto the Z axis as well and leave the y alone. I tried playing with some of the overs of values in the Vector3’s through out the code but with undesired results. any ideas?
I solved this, mostly by systematically moving variables around within the existing code until it worked the way I wanted it to. You’ll note that I included variables for sensitivity and min max values for all 3 axis but the only ones I know work are the x and Z axis so I’m not sure it has the versatility it appears to. I added my own RotationAxis.MouseXAndZ section which is what I actually use in my code. Hopefully this might be able to help someone else figure out a similar issue.
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1,MouseXAndZ = 3, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float sensitivityZ = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
public float minimumZ = -60F;
public float maximumZ = 60F;
float rotationX = 0F;
float rotationY = 0F;
float rotationZ = 0F;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (axes == RotationAxes.MouseXAndY)
{
rotationX = transform.localEulerAngles.y + Input.GetAxis("Horizontal") * sensitivityX;
rotationY += Input.GetAxis("Vertical") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY,rotationX,0);
}
else if (axes == RotationAxes.MouseXAndZ)
{
rotationX += Input.GetAxis("Horizontal") * sensitivityX;
rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
rotationZ += Input.GetAxis("Vertical") * sensitivityZ;
rotationZ = Mathf.Clamp (rotationZ, minimumZ, maximumZ);
transform.localEulerAngles = new Vector3(rotationZ,transform.localEulerAngles.y,-rotationX);
//GetComponentInChildren
}
else
{
rotationY += Input.GetAxis("Vertical") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
void LateUpdate()
{
}
}
This section particularly:
else if (axes == RotationAxes.MouseXAndZ)
{
rotationX += Input.GetAxis("Horizontal") * sensitivityX;
rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
rotationZ += Input.GetAxis("Vertical") * sensitivityZ;
rotationZ = Mathf.Clamp (rotationZ, minimumZ, maximumZ);
transform.localEulerAngles = new Vector3(rotationZ,transform.localEulerAngles.y,-rotationX);
//GetComponentInChildren
}
You can see how I mostly took what was done in the MouseXAndY section and replaced the values with desired axis and related values. I did it wrong several times before finding the right combination.