Search Unity

Show mouse pointer in the screen game

Discussion in 'Scripting' started by Fernando77Cc, Mar 19, 2017.

  1. Fernando77Cc

    Fernando77Cc

    Joined:
    Feb 1, 2017
    Posts:
    22
    Hello and thanks for the forum.
    I am beginnig in the unity programming and I wanted to ask you a question. I am developing a game with unity (5.5), it is a first person point and click. Well, i want when i run the game the cursor or pointer mouse be shown in the game screen to be able to have a reference to click in some objets. Without cursor i dont know where i am doing click. I have looked around forums and i have seen about "Cursor.Visible=true", i have tried write it in the scripts (i dont know which and i have written it in alls) in the start function but i doesnt work.
     
    Last edited: Mar 19, 2017
  2. Vectorbox

    Vectorbox

    Joined:
    Jan 27, 2014
    Posts:
    232
    Assuming that you're coding in C# the following should work. You only need to add the line to one script.

    Code (csharp):
    1.  
    2. void Start () {
    3.         Cursor.visible = true;
    4.     }
    5.  
    Also note that in 'CursorLockMode.Locked', the cursor will be invisible regardless of the above setting.
     
    linenum and Patrichor like this.
  3. Fernando77Cc

    Fernando77Cc

    Joined:
    Feb 1, 2017
    Posts:
    22
    yes i am using C#. But it doesnt work. I have already tried it.
    this is my script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class GameManager : MonoBehaviour
    {

    //creamos variable de la propia clase
    public static GameManager ins;
    //variable que almacena el objeto con el que se esta interactuando en el momento
    [HideInInspector]
    public Node currentNode;
    //variable que hace referencia al objeto cameraRig que genera el movimiento de la camara
    public CameraRig rig;

    void Start()
    {

    Cursor.visible = true;
    Cursor.lockState = CursorLockMode.None;
    }

    void Awake()
    {
    ins = this;
    }

    void Update()
    {
    //si hacemos clic con el boton derecho y el nodo con el script prop (bola o cubo) no sea nulo
    //llamamos al metodo arrive para ir hacia atras por medio de la variable loc que almacena
    //la posicion de la mesa, con lo que volvemos a la mesa
    if (Input.GetMouseButtonDown(1) && currentNode.GetComponent<Prop>() !=null) {
    currentNode.GetComponent<Prop>().loc.Arrive();
    }

    }
    }
     
    Last edited: Mar 19, 2017
  4. IndieForger

    IndieForger

    Joined:
    Dec 31, 2012
    Posts:
    92
    Fernando77Cc likes this.
  5. Fernando77Cc

    Fernando77Cc

    Joined:
    Feb 1, 2017
    Posts:
    22
  6. Fernando77Cc

    Fernando77Cc

    Joined:
    Feb 1, 2017
    Posts:
    22
    i have achieved it !!

    i had to write it in this function:

    void OnGUI()
    {
    Cursor.lockState = CursorLockMode.None;
    Cursor.visible = true;
    }

    Now it makes some strange, cursor blinks if i move it. Even so, it is more than nothing ..
     
    Last edited: Mar 19, 2017
  7. IndieForger

    IndieForger

    Joined:
    Dec 31, 2012
    Posts:
    92
    Hey @Fernando77Cc. It is pretty weird. I am not sure if that would be right guess but my guess is that you might have `Cursor.lockState` or `Cursor.visible` modified from different places. If you still have the issue please try to global search for those two. It seems to me that the blink occurs because you are switching it ON / OFF in different places in your code perhaps.
     
  8. BlueRain01

    BlueRain01

    Joined:
    Aug 22, 2010
    Posts:
    86
    I have the same issue with Unity 2017 , the mouse keep blinking and looked on the center .
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    @okba28mca : I would have to 'guess' that this is almost certainly because of a script(s) you are using -- blocking the cursor. Often FPS games do that so you can move around.. if it's blinking, maybe you wrote code to show the cursor? I'm not sure.
    The most appropriate solution would be to find the script and modify it so you can get out of that mode.
    I have seen a number of examples where hitting 'escape' (in FPS scripts) brings you out of that hidden cursor mode until you click back in the game (not the UI, usually).. Hopefully that makes sense.
     
  10. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Do a search in your code for any instances using the Cursor namespace. There's got to be something that's trying to mess with the cursor. I.e. do a solution-wide search for "Cursor".

    I know for sure the cursor behaves fine in my Windows standalone app, and it's on (visible) by default. The only time anything weird happens is when separate code start trying to modify the Cursor.visible state, and step on each others toes.
     
  11. gcsonka

    gcsonka

    Joined:
    Jan 13, 2018
    Posts:
    2
    I had the same issue. I found the following code in the MouseLook.cs script and commended out the lines that made the cursor invisible. Works fine , cursor is visible and not blinking.

    private void InternalLockUpdate()
    {
    if(Input.GetKeyUp(KeyCode.Escape))
    {
    m_cursorIsLocked = false;
    }
    else if(Input.GetMouseButtonUp(0))
    {
    m_cursorIsLocked = true;
    }

    if (m_cursorIsLocked)
    {
    //Cursor.lockState = CursorLockMode.Locked;
    //Cursor.visible = false;
    }
    else if (!m_cursorIsLocked)
    {
    Cursor.lockState = CursorLockMode.None;
    Cursor.visible = true;
    }
    }
     
  12. gcsonka

    gcsonka

    Joined:
    Jan 13, 2018
    Posts:
    2
    Also .... Just un checking "Lock Cursor" in the FPS script is a nicer way of achieving the same thing as the script above.
    upload_2018-1-13_0-10-52.png
     
  13. moontowhee

    moontowhee

    Joined:
    Apr 2, 2019
    Posts:
    1
    The cursor is now also in the player screen. How do you remove it to be only visible on, let's say, a panel/menu?
     
  14. Sylwestro9494

    Sylwestro9494

    Joined:
    May 3, 2020
    Posts:
    1
    cooooolllll WORKS !
     
  15. Boombox100jr

    Boombox100jr

    Joined:
    Apr 16, 2022
    Posts:
    1
    hey just use
    Cursor.lockState = CursorLockMode.Locked;
    to hide your mouse