Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
avatar image
0
Question by FritzPeace · Jul 22, 2015 at 04:56 AM · c#2dmouseclamp

Clamp a mouse around several 2d objects

I am doing a sort of Duck Shooting game, and I have set my whole environment. However the "environment" is several curtains. I want the mouse to stay in the area in between them.

How would I do this?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cherno · Jul 22, 2015 at 06:37 AM

It's a little complicated since Unity doesn't have such a function, you have to dive into user32.dll stuff.

This thread details the method:

Constrain Mouse Cusor While Fullscreen (Windows)

 using UnityEngine;
 using System.Collections;
 using System.Runtime.InteropServices;
 
 public class ClipCursorScript : MonoBehaviour
 {
     [DllImport( "user32.dll", CharSet = CharSet.Auto, ExactSpelling = true )]
     [return: MarshalAs( UnmanagedType.Bool )]
     public static extern bool ClipCursor( ref RECT rcClip );
     [DllImport( "user32.dll" )]
     [return: MarshalAs( UnmanagedType.Bool )]
     public static extern bool GetClipCursor( out RECT rcClip );
     [DllImport( "user32.dll" )]
     static extern int GetForegroundWindow( );
     [DllImport("user32.dll")]
     [return: MarshalAs( UnmanagedType.Bool )]
     static extern bool GetWindowRect( int hWnd, ref RECT lpRect );
     
     [StructLayout( LayoutKind.Sequential )]
     
     public struct RECT
     {
         public int Left;
         public int Top;
         public int Right;
         public int Bottom;
         public RECT( int left, int top, int right, int bottom )
         {
             Left = left;
             Top = top;
             Right = right;
             Bottom = bottom;
         }
     }
     
     RECT currentClippingRect;
     RECT originalClippingRect = new RECT( );
     
     void Start()
     {
         currentClippingRect = new RECT(Screen.width / 2 - 10, Screen.height / 2 - 10, Screen.width / 2 + 10, Screen.height / 2 + 10);
         ClipCursor( ref currentClippingRect);
 
     }
     
     void OnApplicationQuit()
     {
         ClipCursor( ref originalClippingRect );
     }
 }

The currentClippingRect is the rect that will constrain the cursor. Note that it will only work in full screen mode, as it uses screen coordinates of the WHOLE screen, not just what Unity would regard as a screen (the window where the game is ran).

Comment
Add comment · Show 2 · 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 FritzPeace · Jul 24, 2015 at 12:33 AM 0
Share

Thanks, but I may need a different solution. I am not experient enough for codes like this :P. Ill try to figure out the code etc, Or try another solution. Couldn't I perhaps make the mouse not be able to pass by certain colliders etc?

avatar image Cherno · Jul 24, 2015 at 06:11 AM 0
Share

No. Unity only supports locking the mouse to the center of the screen. If you use the script above, you have to find the screen coordinates and width + length of the rect in which the cursor should stay and use those values to define the currentClippingRect.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

If i click on a object i move to the next level (c#) 0 Answers

Create objects on a mouse click in 2D 1 Answer

Make a dotted line from an object to the mouse 2d 2 Answers

Apply force towards mouse click 2D C# 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