Disable mouse input during pause or inventory

mattis Posts: 69Member
Heres a little script I manage to get to work pretty good if you want to toggle the camera movement and mouse input/cursor on button press.. ex; I push "tab" inventory pops up, i can now move my mouse and see the cursor and press on gui stuff without shooting ...


Here you go!

//AstralStorm Games Co-op MattGC & partly credits to handuk420

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class VisibleCursor : MonoBehaviour
{



public bool freeze = false;

private vp_FPCamera Camera;
private vp_FPPlayerEventHandler Player;
private vp_FPInput _Input;
private vp_FPController Controller;

void Awake()
{



Player = GameObject.FindObjectOfType(typeof(vp_FPPlayerEventHandler)) as vp_FPPlayerEventHandler;
Camera = GameObject.FindObjectOfType(typeof(vp_FPCamera)) as vp_FPCamera;
_Input = GameObject.FindObjectOfType(typeof(vp_FPInput)) as vp_FPInput;
Controller = GameObject.FindObjectOfType(typeof(vp_FPController)) as vp_FPController;
_Input.MouseCursorForced = false;
}



void Update()
{
if (Input.GetKeyDown(KeyCode.Tab))
{
freeze = !freeze;

if (freeze == true)
{


_Input.MouseCursorForced = true;
vp_LocalPlayer.Camera.SetState("Freeze", true);
vp_LocalPlayer.InputManager.MouseCursorForced = true;
vp_Utility.LockCursor = true;

}
else
{
_Input.MouseCursorForced = false;
vp_LocalPlayer.Camera.SetState("Freeze", false);
vp_LocalPlayer.InputManager.MouseCursorForced = false;
vp_Utility.LockCursor = false;

}


}


}
}

Answers

  • TryThisTryThis Posts: 887Member
    edited September 4
  • BrandSpankingNewBrandSpankingNew Posts: 2,321Member, MPTester
    edited September 4
    There are other solutions here on the forum that may be less, uhhh, tacky, for this simple problem.
    Beautiful objects are wrought by study through effort, but ugly things are reaped automatically without toil.
    Democritus
  • mattis Posts: 69Member
    well actually I have only found solutions that are as tacky or more... please give me an example then
  • TryThisTryThis Posts: 887Member
    Tacky is not necessarily bad. What you have shown WILL work, but it's tacky!
    For example ...
    Why are you using this in your example?

    using UnityEngine.UI;

    Why are you doing all of this in your example?

    void Awake()
    {
    Player = GameObject.FindObjectOfType(typeof(vp_FPPlayerEventHandler)) as vp_FPPlayerEventHandler;
    Camera = GameObject.FindObjectOfType(typeof(vp_FPCamera)) as vp_FPCamera;
    _Input = GameObject.FindObjectOfType(typeof(vp_FPInput)) as vp_FPInput;
    Controller = GameObject.FindObjectOfType(typeof(vp_FPController)) as vp_FPController;
    _Input.MouseCursorForced = false;
    }

    Why are you doing your MouseCursorForced things twice in your example, once directly and once via vp_LocalPlayer?

    _Input.MouseCursorForced = true;
    vp_LocalPlayer.InputManager.MouseCursorForced = true;


    _Input.MouseCursorForced = false;
    vp_LocalPlayer.InputManager.MouseCursorForced = false;

    These things are tacky, IMHO. So, I'd say that the word was used accurately by BSN.
  • mattis Posts: 69Member
    In awake I need all those or else it wont work.. it only complained on things without them...

    the mousecursor force things that are duplicated is tacky.. you are right.. only need to use one of them.
  • TryThisTryThis Posts: 887Member
    In awake I need all those or else it wont work.. it only complained on things without them...
    Well, that is complete horseshit, as anyone who would run your tacky example can discover for themselves! Everything one needs to do what you said your example would do ... (
    Heres a little script I manage to get to work pretty good if you want to toggle the camera movement and mouse input/cursor on button press.. ex; I push "tab" inventory pops up, i can now move my mouse and see the cursor and press on gui stuff without shooting ...
    ) .. can do exactly the same thing, without any of your tacky misunderstanding bad code practices thingy, by simply using vp_LocalPlayer and vp_Utility. Additionally, nowhere in your example did you show anything about accessing inventory ( notice that you said that when you press tab that your inventory pops up! ha!).
    it only complained
    ??

    Huh?, say WHAT? What is "it". Oh, wait, I get it!! You decided to just slap some tacky crap into the scripts and mods section without thinking it through?! I think that's it! Go soak. Let this go. You did a really good thing by bringing your example to the forum, and I am sure that we all thank you for it. Peace. I can't wait to see your next helpful example. I know it will be better. *puts sword away*

  • mattis Posts: 69Member
    yes, thank you for clearifying that my script sucks..
  • JustinJustin Posts: 307Member, Administrator, Moderator
    Thank you @mattis for sharing your script - by putting your scripts out in the open you do open yourself up to some harsh comments but you can also learn a lot. In your script you are storing a reference to the camera, player and controller which it doesn't look like you use. If you remove those references you'll be able to clean your script up a bit.

    Everybody had to start somewhere and my suggestion would be to keep with it and learn as much as you can. Unity's Learn site has become a great resource and can help you navigate the Unity API as well as learn some new c# concepts.

    [ UFPS Docs ] . . . [ F.A.Q. ] . . . [ Tutorials ] . . . [ Knowledgebase ]
    [ Games Showcase ] . . . [ Get it now! ]

    [home] [twitter] [youtube]
  • mattis Posts: 69Member
    Thank you.. I just removed the unused references.
  • mattis Posts: 69Member
    and theres a tiny bit thats is grindin my gears.. I have to right click to get themouse look back and cursor hidden. I understand that the ESC key works that way too.
  • mattis Posts: 69Member
    I am new to this so I dont need people to be rude.. at least I get stuff to do what I want.. WHY is tought the way is this ;

    private vp_FPCamera Camera; = So i can set camera state to freeze.
    private vp_FPPlayerEventHandler Player; Tought I needed it because it is this player (seems i dont) (DELETED)
    private vp_FPInput _Input: = So I can force mouse cursor....
    private vp_FPController Controller; = I didnt need this either so (DELETED)

    This is the new ;




    //AstralStorm Games Co-op MattGC & partly credits to handuk420

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;

    public class VisibleCursor : MonoBehaviour
    {

    public bool freeze = false;
    private vp_FPCamera Camera;
    private vp_FPInput _Input;

    void Awake()
    {
    Camera = GameObject.FindObjectOfType(typeof(vp_FPCamera)) as vp_FPCamera;
    _Input = GameObject.FindObjectOfType(typeof(vp_FPInput)) as vp_FPInput;
    _Input.MouseCursorForced = false;
    }

    void Update()
    {
    if (Input.GetKeyDown(KeyCode.Tab))
    {
    freeze = !freeze;

    if (freeze == true)
    {
    _Input.MouseCursorForced = true;
    vp_LocalPlayer.Camera.SetState("Freeze", true);
    vp_LocalPlayer.InputManager.MouseCursorForced = true;
    vp_Utility.LockCursor = true;
    }
    else
    {
    _Input.MouseCursorForced = false;
    vp_LocalPlayer.Camera.SetState("Freeze", false);
    vp_LocalPlayer.InputManager.MouseCursorForced = false;
    vp_Utility.LockCursor = false;

    }
    }
    }
    }
  • mattis Posts: 69Member
    So.. It works now.. Like the ESC key.. When I´ve been in the inventory and hits "tab" I have to right mouse click to get control...

    @TryThis : The reason why I didnt mention the inventory it´s because this is just the script that handles the cursorShowing/Hiding.. The inventory logic is on another script ofcourse...where the "Hit tab and show inventory thingy is)....

    So my apologies for you sir!
  • mattis Posts: 69Member
    And I know... There is probably a better way.. but this works so... That why games have a bug report button... and yeah when I said this you probably gonna say " Learn this coding the right and best way from the beginning" yea, but I dont have time to sit a week on a little thing..
  • mattis Posts: 69Member
    *Puts my sunglasses on and opens a beer*
Sign In or Register to comment.