Jump to content

Unity Engine - Mouse cursor locks when any Key is pressed.

My problem is that in my unity game when you press a key the mouse will freeze. I want to use the mouse input and while a key is being held down (imagine something like an FPS camera). I think that issue is that the mouse is being locked when the key is pressed, but I can't work out how to stop it from happening. This happens across projects, so I think that it isn't an issue with my code/ something I've done, but just in case here is some code that I think is relevant:

public class MouseMovement : MonoBehaviour {
  
    public GameObject myObject;
    public float speed;
	
	void Update () 
    {
        myObject.transform.Rotate(Input.GetAxis("Mouse Y") * Time.deltaTime * speed, 0.0f, 0.0f);
      	/// handle other axis in same way.
	}
}

and :

public class KeybourdMovement : MonoBehaviour {

    public GameObject myObject;
    public float speed;
  
	void Update ()
    {
        if (Input.anyKey)
        {
            if (Input.GetKey(KeyCode.W))
            {
                myObject.transform.Translate(Vector3.forward * Time.deltaTime * speed);
            }
          //handle the ASD in the same way
        }
    }

As a side note I'm using Unity 5.5.0f3.

So if somebody could point me in the right direction, it would would very helpful.

!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!

Link to comment
Share on other sites

Link to post
Share on other sites

25 minutes ago, DiscoLights2001 said:

So if somebody could point me in the right direction, it would would very helpful.

You think you can just make a pun like that and get away with it?

i7-4790k | MSI Z97 GAMING-5 | Corsair Vengeance 16 GB | Samsung EVO-850 250GB SSD & WD blue 1 TB HDD | EVGA 1070 SC | Red NZXT H440 | Cooler Master G650W

 

Link to comment
Share on other sites

Link to post
Share on other sites

Hmm. I tried to replicate your problem but your code seems to work for me. However, I'm wondering why you have the (Input.anyKey) statement in place for the keyboard script, as it seems kind of pointless unless I'm missing something (not the greatest Unity developer here). The code worked just fine without that script. Also, you can put all the controls into one script instead of having two. <- actually that isn't ideal

 

Try omitting the if (Input.anyKey) statement to see if that has an effect.

i7-4790k | MSI Z97 GAMING-5 | Corsair Vengeance 16 GB | Samsung EVO-850 250GB SSD & WD blue 1 TB HDD | EVGA 1070 SC | Red NZXT H440 | Cooler Master G650W

 

Link to comment
Share on other sites

Link to post
Share on other sites

You must have other scripts. 

 

Are these all of them? 

 

Post whole scripts. 

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, DiscoLights2001 said:

My problem is that in my unity game when you press a key the mouse will freeze. I want to use the mouse input and while a key is being held down (imagine something like an FPS camera). I think that issue is that the mouse is being locked when the key is pressed, but I can't work out how to stop it from happening. This happens across projects, so I think that it isn't an issue with my code/ something I've done, but just in case here is some code that I think is relevant:


public class MouseMovement : MonoBehaviour {
  
    public GameObject myObject;
    public float speed;
	
	void Update () 
    {
        myObject.transform.Rotate(Input.GetAxis("Mouse Y") * Time.deltaTime * speed, 0.0f, 0.0f);
      	/// handle other axis in same way.
	}
}

and :


public class KeybourdMovement : MonoBehaviour {

    public GameObject myObject;
    public float speed;
  
	void Update ()
    {
        if (Input.anyKey)
        {
            if (Input.GetKey(KeyCode.W))
            {
                myObject.transform.Translate(Vector3.forward * Time.deltaTime * speed);
            }
          //handle the ASD in the same way
        }
    }

As a side note I'm using Unity 5.5.0f3.

So if somebody could point me in the right direction, it would would very helpful.

monobehaivor to stereo?

or update=classmouse&&classkeyboard every 1ms

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks to all those that offered help (also I'm glad someone saw the pun too). I have now discovered the problem. For those who are interested in the issue: As it turns out the problem is with me, and not with the program (I'm am not very observant basically).  The issue is that I am on a laptop (I'm not normally) and this laptop for some reason, turns of the touch pad when ever a key is pressed. I never noticed it before. Thanks for your patients and help.

!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×