Disable mouse input during pause or inventory

mattis Posts: 91Member
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: 974Member
    edited September 2017
  • BrandSpankingNewBrandSpankingNew Posts: 2,362Member, MPTester
    edited September 2017
    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: 91Member
    well actually I have only found solutions that are as tacky or more... please give me an example then
  • TryThisTryThis Posts: 974Member
    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: 91Member
    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: 974Member
    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: 91Member
    yes, thank you for clearifying that my script sucks..
  • JustinJustin Posts: 356Member, 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: 91Member
    Thank you.. I just removed the unused references.
  • mattis Posts: 91Member
    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: 91Member
    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: 91Member
    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: 91Member
    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: 91Member
    *Puts my sunglasses on and opens a beer*
  • robtbanister Posts: 1Member
    edited May 27
    AND OF COURSE, some RUDE person(s) is going to have something to say about MY COMMENT!!! For that matter, the owner of this website, is not keeping up with the CAPTCHA website, cause this version of CAPTCHA is DOWN, so you are not updating your website with the latest code, if you want to be efficient, DUDE!!!
    Well, after reading thru this THREAD in this forum, I just could not get past all the type of comments I am finding... How is it that people have to be ROOD and disrespectful to people looking for answers to what is going on in UFPS. (and I won't mention anyone's name in here. You know who you are)...
    Have we all forgotten where we came from. Everyone has an EGO about programming, but this UFPS code, although, pretty well documented, is still, as someone puts it, "AN OCEAN OF CODE". Yes, the code works great out of the box, but geezz, the people that designed this DID start somewhere, in developing this. They weren't just born with this code developed.
    Yes, it is up to each individual, to study the code, in order to "MOD" something to the way they want it, but have we forgotten what it is like to learn programming, and code???
    I might suggest that anyone that decides they want to be RUDE, or disrespectful to people looking for answers, REFRAIN from acting like an IDIOT, and just get along with the program.
    People come to these forums, looking for help, and if someone is going to OFFER some help, then simply offer up the help, and REFRAIN from thinking you are a GOD. This programming can be like studying CHEMISTRY...
    Here is an IDEA that someone might develop, as far as making things clearer on this UFPS...
    How about developing a visual CHART as to EVERYTHING that is going on with it... If you were to do THAT, then you probably wouldn't have to deal with ANSWERING QUESTIONS from people that are trying to understand this OCEAN OF CODE...
    Geezz, people... take a break and BREATH. It is only programming, and a HOBBY.
    Think about it this way... Anyone that needs to be RUDE, is simply INSECURE in their life, and programming, and that is why the RUDE comes out. They have to put down other people, to build themselves up!!!
    =================================
    I came back here the next day to UPDATE MY COMMENTS...
    How dare someone make such a comment as TACKY CODE. FIRST OF ALL, this is "YOUR" opinion... What kind of MORON are you. Do you not understand the concept of "TRYING TO PROGRAM SOMETHING". I suspect that, because of this "OCEAN OF CODE", Mattis was trying something, and forgot to make the changes. I do this all the time, and when trying something, you often forget the code previously entered, only to find it later when you go back and REVIEW what you have done. AND besides that, he was submitting his code, to SHARE with other people, so WHY the HELL would someone want to COMPLAIN about something someone was SHARING. BRAINLESS BUTT, you are trying to make this forum so that NO-ONE wants to share anything, because of your STUPID RUDE COMMENTS about what, in your HUMBLE OPINION, was "TACKY CODE". This is a VERY STUPID way to REACT to what someone posted.
    YOU need to BACK OFF, and quit thinking you are the END ALL BE ALL programmer. Just because you have studied the CODE for UFPS, does not give you the right to be an ASSHOLE towards other people. NOW YOU HAVE MY HUMBLE OPINION, IDIOT!!! This kind of CRAP, pisses me off so much, that... well, I won't go into that... Do you not understand the concept of.. If you can not say something nice, DON'T SAY ANYTHING. Constructive comments are the best way to go... and for that matter, have you seen the image of the guy that talks about HORSESHIT... I guess, now, we understand why he made the comments he did... LOL
    MATTIS, I totally respect your effort on your code. THANKS FOR SHARING. Just ignore this idiot, and keep doing what your doing. I appreciate your sharing your code. Thanks for that!!!
    Geezz, do I really have to explain that???
  • mattis Posts: 91Member

    AND OF COURSE, some RUDE person(s) is going to have something to say about MY COMMENT!!! For that matter, the owner of this website, is not keeping up with the CAPTCHA website, cause this version of CAPTCHA is DOWN, so you are not updating your website with the latest code, if you want to be efficient, DUDE!!!
    Well, after reading thru this THREAD in this forum, I just could not get past all the type of comments I am finding... How is it that people have to be ROOD and disrespectful to people looking for answers to what is going on in UFPS. (and I won't mention anyone's name in here. You know who you are)...
    Have we all forgotten where we came from. Everyone has an EGO about programming, but this UFPS code, although, pretty well documented, is still, as someone puts it, "AN OCEAN OF CODE". Yes, the code works great out of the box, but geezz, the people that designed this DID start somewhere, in developing this. They weren't just born with this code developed.
    Yes, it is up to each individual, to study the code, in order to "MOD" something to the way they want it, but have we forgotten what it is like to learn programming, and code???
    I might suggest that anyone that decides they want to be RUDE, or disrespectful to people looking for answers, REFRAIN from acting like an IDIOT, and just get along with the program.
    People come to these forums, looking for help, and if someone is going to OFFER some help, then simply offer up the help, and REFRAIN from thinking you are a GOD. This programming can be like studying CHEMISTRY...
    Here is an IDEA that someone might develop, as far as making things clearer on this UFPS...
    How about developing a visual CHART as to EVERYTHING that is going on with it... If you were to do THAT, then you probably wouldn't have to deal with ANSWERING QUESTIONS from people that are trying to understand this OCEAN OF CODE...
    Geezz, people... take a break and BREATH. It is only programming, and a HOBBY.
    Think about it this way... Anyone that needs to be RUDE, is simply INSECURE in their life, and programming, and that is why the RUDE comes out. They have to put down other people, to build themselves up!!!
    =================================
    I came back here the next day to UPDATE MY COMMENTS...
    How dare someone make such a comment as TACKY CODE. FIRST OF ALL, this is "YOUR" opinion... What kind of MORON are you. Do you not understand the concept of "TRYING TO PROGRAM SOMETHING". I suspect that, because of this "OCEAN OF CODE", Mattis was trying something, and forgot to make the changes. I do this all the time, and when trying something, you often forget the code previously entered, only to find it later when you go back and REVIEW what you have done. AND besides that, he was submitting his code, to SHARE with other people, so WHY the HELL would someone want to COMPLAIN about something someone was SHARING. BRAINLESS BUTT, you are trying to make this forum so that NO-ONE wants to share anything, because of your STUPID RUDE COMMENTS about what, in your HUMBLE OPINION, was "TACKY CODE". This is a VERY STUPID way to REACT to what someone posted.
    YOU need to BACK OFF, and quit thinking you are the END ALL BE ALL programmer. Just because you have studied the CODE for UFPS, does not give you the right to be an ASSHOLE towards other people. NOW YOU HAVE MY HUMBLE OPINION, IDIOT!!! This kind of CRAP, pisses me off so much, that... well, I won't go into that... Do you not understand the concept of.. If you can not say something nice, DON'T SAY ANYTHING. Constructive comments are the best way to go... and for that matter, have you seen the image of the guy that talks about HORSESHIT... I guess, now, we understand why he made the comments he did... LOL
    MATTIS, I totally respect your effort on your code. THANKS FOR SHARING. Just ignore this idiot, and keep doing what your doing. I appreciate your sharing your code. Thanks for that!!!
    Geezz, do I really have to explain that???


    Thanks for the backup brother! I totally rewrote that code by the way to something shorter.. You were right that I left alot of unesscary things, I forger about them all the time and then the code runs slow, sometime alot of debug logs too.. I recently made a custom muzzle flash that is compatible with shuriken if you need it its here on the forum somewhere..
Sign In or Register to comment.