Search Unity

Question Cursor Input For Look (new input system) - Enable Disable inside a script

Discussion in 'Getting Started' started by MagicBlu, Mar 22, 2022.

  1. MagicBlu

    MagicBlu

    Joined:
    May 17, 2018
    Posts:
    3
    Hi,

    I'm looking to enable and disable the "Cursor Input For Look" when a script is triggered.

    I wrote this code to hide and unhide the cursor.

    Any help is really appreciated.

    Unity.png

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class CursorBehaviour : MonoBehaviour
    7. {
    8.     [SerializeField] private Canvas myCanvas;
    9.  
    10.     void OnTriggerEnter(Collider other)
    11.     {
    12.         if (other.CompareTag("Player"))
    13.         {
    14.             print("GameObject is Active");
    15.             Cursor.visible = true;
    16.             Cursor.lockState = CursorLockMode.None;
    17.         }
    18.     }
    19.  
    20.     void OnTriggerExit(Collider other)
    21.     {
    22.         if (other.CompareTag("Player"))
    23.         {
    24.             print("GameObject is Inactive");
    25.             Cursor.visible = false;
    26.             Cursor.lockState = CursorLockMode.Locked;
    27.         }
    28.     }
    29. }
    30.  
    31.