Search Unity

Diabling MouseLook X and Y, whilst keep cursor active, on trigger.

Discussion in 'Scripting' started by cristo, Jul 5, 2015.

  1. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    I'm trying to lock the player camera from rotating up and down or left and right whilst the cursor remains active after walking into a trigger.

    Any advice about solving this problem would be awesome.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraLock : MonoBehaviour {
    5.  
    6.     public GameObject Camera;
    7.     public bool mouseLook = enabled;
    8.  
    9.     void Start (){
    10.         Camera = GameObject.Find ( "Main Camera" );
    11.         Camera.GetComponentsInChildren<MouseLook>().enabled = true;
    12.         }
    13.  
    14.     public void OnTriggerEnter (){
    15.         Camera.GetComponentsInChildren<MouseLook>().enabled = false;
    16.  
    17.        
    18.     }
    19.  
    20.     public void OnTriggerExit () {
    21.         Camera.GetComponentsInChildren<MouseLook>().enabled = true;
    22.  
    23.     }
    24. }
    25.  
    26.  
     
  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    What you are doing should work as long as the mouselook script depends on one of the update methods (Update, FixedUpdate, etc...)

    Otherwise, in your mouselook script you can have a bool "mouseDisabled" and before you do your mouselook code, you do
    "if(mouseDisabled) return;"

    Then in your ontriggerenter and exit, just set the moiseDisabled bool to true or false.
     
    cristo likes this.
  3. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Thanks HiddenMonk, thanks for the reply, I'll do my best to understand and implement that.