Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by Flickayy · Jun 14, 2013 at 10:10 PM · c#mouse input

How to stop movement, when mouse is off screen.

I'm hoping there's someone out there that can help me with a small problem.

Currently I have an Input Manager attached to the main camera to allow the user to pan around the map by moving the mouse to the edges of the window, but I've encountered a slight problem which I've tried to fix myself to no avail.

If the mouse goes outside of the window, the panning still happens, which I find irritating when I'm debugging or using other applications. So I am hoping that someone can help me to stop the movement happening when the mouse is outside the game window.

Here is the code for my Input Manager.

 using UnityEngine;
 using System.Collections;
 
 public class InputManager : MonoBehaviour {
 
     public Vector3 position = new Vector3(0,0, -10);
     public int boundary = 50;
     public int speed = 4;
 
     private int screenBoundsWidth;
     private int screenBoundsHeight;
 
     // Use this for initialization
     void Start()
     {
         screenBoundsWidth = Screen.width;
         screenBoundsHeight = Screen.height;
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.mousePosition.x > screenBoundsWidth - boundary) {
             position.x += speed * Time.deltaTime;
         }
 
         if (Input.mousePosition.x < 0 + boundary) {
             position.x -= speed * Time.deltaTime;
         }
 
         if (Input.mousePosition.y > screenBoundsHeight - 10) {
             position.y += speed * Time.deltaTime;
         }
 
         if (Input.mousePosition.y < 0 + boundary) {
             position.y -= speed * Time.deltaTime;
         }   
 
         Camera.mainCamera.transform.position = position;
     }
 }

Thank you for your time.

EDIT

I have come up with a hacky work around, but it still causes the movement to happen in certain locations around the outside of the window. I am hoping someone can come up with a better solution.

 if (Input.mousePosition.x  < screenBoundsWidth && Input.mousePosition.y < screenBoundsHeight) {
     if (Input.mousePosition.x > screenBoundsWidth - boundary) {
         position.x += speed * Time.deltaTime;
     }
 }

 if (Input.mousePosition.x > 0 && Input.mousePosition.y > 0) {
     if (Input.mousePosition.x < 0 + boundary) {
         position.x -= speed * Time.deltaTime;
     }
 }

 if (Input.mousePosition.y < screenBoundsHeight && Input.mousePosition.x < screenBoundsWidth) {
     if (Input.mousePosition.y > screenBoundsHeight - 22) {
         position.y += speed * Time.deltaTime;
     }
 }

 if (Input.mousePosition.y > 0 && Input.mousePosition.x > 0) {
     if (Input.mousePosition.y < 0 + boundary) {
         position.y -= speed * Time.deltaTime;
     }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Triqy · Jun 15, 2013 at 12:57 AM 0
Share

A good start to your answer is to look into $$anonymous$$athf.Clamp and some how use this to clamp the mouse to the resolution to the screen

avatar image Flickayy · Jun 15, 2013 at 11:03 AM 0
Share

Ah, thanks I'll have a look into this shortly. I've not looked into $$anonymous$$athf.Clamp.

4 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

Answer by Flickayy · Jun 15, 2013 at 03:54 PM

Solution solved by Jacek Przemieniecki over on Stack Overflow.

3 Ideas:

 Rect screenRect = new Rect(0,0, Screen.width, Screen.height);
 if (!screenRect.Contains(Input.mousePosition))
     return;

The same can be written more verbously as:

 float mouseX = Input.MousePosition.x;
 float mouseY = Input.MousePosition.y;
 float screenX = Screen.width;
 float screenY = Screen.height;
 
 if (mouseX < 0 || mouseX > screenX || mouseY < 0 || mouseY > screenY)
     return;
 
 // your Update body

...which is pretty much the same as your "hacky" solution (which is completely valid imho).

Another option is to create 4 Rect objects for each screen border, then check if mouse is inside those rects. Example:

 public float boundary = 50;
 public float speed = 4;
 private Rect bottomBorder;
 private Rect topBorder;
 private Transform cameraTransform;
 
 private void Start()
 {
     cameraTransform = Camera.mainCamera.transform
     bottomBorder = new Rect(0, 0, Screen.width, boundary);
     topBorder = new Rect(0, Screen.height - boundary, Screen.width, boundary);
 }

 private void Update()
 {
     if (topBorder.Contains(Input.mousePosition))
     {
         position.y += speed * Time.deltaTime;
     }

     if (bottomBorder.Contains(Input.mousePosition))
     {
         position.y -= speed * Time.deltaTime;
     }

     cameraTransform.position = position;
 }

The tricky part here is that Rect coordinates have Y axis pointing down and Input.mousePosition has Y pointing up... so bottomBorder Rect has to be on the top, and topBorder has to be at the bottom. Left and right borders are not affected.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jix · Jul 01, 2014 at 10:41 PM 1
Share

the first one worked like magic for me, thanks for updating your question with the answer

avatar image simonrcodrington · Oct 13, 2018 at 12:47 PM 0
Share

Hey, thanks for that, it's exactly what I was looking for. Now I don't have to worry about the mouse triggering when it's outside the editor during debugging.

avatar image rbosse · Feb 23, 2019 at 10:46 PM 0
Share

Thanks, I used #1.

avatar image varikor · Apr 19, 2019 at 04:40 PM 0
Share

Thanks so much, number one works perfectly for me too :)

avatar image
2

Answer by MrMeows · Mar 18, 2016 at 01:05 AM

As of Unity 5.2, Input.mousePosition is now limited by the screen bounds, so it cannot detect when the cursor is outside of the window. However, Input.GetAxis("Mouse X") and Input.GetAxis("Mouse Y"), or whatever you call them, continue to function outside of the window. If Sensitivity is set to 2, they return the cursor's movement in pixel coordinates. Knowing that, it is easy to find MousePosition.

    Vector2 MousePosition;
    void Update()
    {
        if(Input.mousePosition.y > 0 && Input.mousePosition.y < Screen.height && Input.mousePosition.x > 0 && Input.mousePosition.x < Screen.width)
        {
            MousePosition = Input.mousePosition;
        } else
        {
            MousePosition += new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
        }
    }
Remember: Sensitivity for "Mouse X" and "Mouse Y" must be set to 2.
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AndyUr · Jun 05, 2016 at 04:37 AM 0
Share

This works like a charm! And the step of setting the sensitivity is not really a requirement with this code logic. At least for the purposes of detecting whether the mouse is off-screen, without caring about precision off-screen.

avatar image
0

Answer by Triqy · Jun 15, 2013 at 01:00 AM

Here is another reference point to look at http://wiki.unity3d.com/index.php/Custom_2D_Pointer

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Flickayy · Jun 15, 2013 at 03:29 PM 0
Share

I see this restricts it going off screen, how about stopping movement when the mouse is off screen, any ideas?

I have added in my code a way to clamp, but it doesn't give me the desired effect. Although it will be useful anyway.

         if (Input.mousePosition.y < 0 + boundary) {
             position.y -= speed * Time.deltaTime;
         }
 
         
         position.x = $$anonymous$$athf.Clamp(position.x, -2, 2);
         position.y = $$anonymous$$athf.Clamp(position.y, -2, 2);
 
         Camera.mainCamera.transform.position = position;

I would like for the movement to completely stop when the mouse is not in the game window, but I can't find a way to do this.

avatar image
0

Answer by SkyreGames · Jun 15, 2013 at 04:23 PM

if mouse position = null?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Flickayy · Jun 15, 2013 at 10:53 PM 0
Share

I've tried this, but it's not helped. Thanks for the attempt though.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

Quaternion Smooth Rotation 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges