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

Disable MouseLook w/ Keyboard to use UI

Discussion in 'UGUI & TextMesh Pro' started by WoodsBagot, Oct 21, 2014.

  1. WoodsBagot

    WoodsBagot

    Joined:
    Mar 6, 2014
    Posts:
    8
    Newbie here. Using default First Person Controller prefab.

    Whenever I move my mouse over to the screen-space UI, the camera obviously moves. I want to use a keyboard toggle to turn MouseLook off and on as needed. I know MouseLook exists in both the First Person Controller and the actual Camera.

    Is there a script that will do this already? I have seen numerous threads on the topic, but all are partial or don't seem to work (perhaps I am missing something). Anyone have a good one they can share?
     
  2. DrSnake

    DrSnake

    Joined:
    Oct 17, 2014
    Posts:
    33
    Just from the top of my head, didnt test it.
    There are probally easier / more efficient ways to do it, but this should work for now. Make sure to change the line:
    Code (CSharp):
    1. if (Input.GetKey(KeyCode.P)) {
    To your preferred Key

    Create a new script named: DisableMouseLook (Or something different, but then make sure to rename it in the script as well). Attach it to your FPS Controller:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class DisableMouseLook : MonoBehaviour {
    4.     MouseLook mouseLookController;
    5.     MouseLook mouseLookCamera;
    6.  
    7.     void Start () {
    8.         mouseLookController = GetComponent<MouseLook> ();
    9.         mouseLookCamera = Camera.main.GetComponent<MouseLook> ();
    10.     }
    11.  
    12.     void Update () {
    13.         if (Input.GetKey(KeyCode.P)) {
    14.             mouseLookController.enabled = false;
    15.             mouseLookCamera.enabled = false;
    16.         } else {
    17.             mouseLookController.enabled = true;
    18.             mouseLookCamera.enabled = true;
    19.         }
    20.     }
    21. }
    This code works as such:
    While holding down the letter P, the mouselook components are disabled.
     
    WoodsBagot likes this.
  3. WoodsBagot

    WoodsBagot

    Joined:
    Mar 6, 2014
    Posts:
    8
    Perfect!
     
  4. shaneburger

    shaneburger

    Joined:
    Oct 10, 2014
    Posts:
    15
    Hmmm... mouseLook doesn't seem to be working any more in Unity 5. Anyone have an idea why?
     
  5. shaneburger

    shaneburger

    Joined:
    Oct 10, 2014
    Posts:
    15
    Anyone have an idea on this?