Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

I need some help with the mouse look

Discussion in 'Scripting' started by DrunkProgrammer, Mar 11, 2015.

  1. DrunkProgrammer

    DrunkProgrammer

    Joined:
    Mar 7, 2015
    Posts:
    12
    using UnityEngine;
    using System.Collections;
    [AddComponentMenu("Camera-Control/Mouse Look")]
    public class MouseLook : 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;

    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);
    }
    }

    void Start ()
    {
    if (GetComponent<Rigidbody>())
    GetComponent<Rigidbody>().freezeRotation = true;
    }
    }


    This code makes the camera to move in the Y axis, as well. How can I block this movement?
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    Please use code tags when posting code. It makes reading and debugging so much easier.

    If you want the camera to not be affected by Mouse Y movements, look in the code for references to Mouse Y and comment out, modify, or remove those lines. If I recall correctly with this script, you can just comment out any line that assigns a value to rotationY and the camera will stay at the same Y value the whole time.
     
  3. DrunkProgrammer

    DrunkProgrammer

    Joined:
    Mar 7, 2015
    Posts:
    12
    And what should I do in this case; transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
    ?