Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by MrMiyagi · Dec 06, 2017 at 05:39 AM · mousecursor

Limit Mouse movement around player

I'm working on a 2d top-down shooter, where the player aims using the mouse. I'm trying to limit the mouse movement around the player.

alt text

I'm using C# for scripting

EDIT: I forgot to mention that I don't need to use the hardware cursor. Currently in my project I'm hiding the cursor and using a GameObject that follows the mouse input as the crosshair.

image.png (3.5 kB)
Comment
Add comment · Show 3
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 NorthStar79 · Dec 06, 2017 at 08:41 AM 0
Share

First, you can not really control mouse in unity by native, you need to access hardware mouse which can be very tricky for different OS. I can suggest this asset for this task. if you use this asset this would be your sample code :

 Vector2 PlayerPosition = Vector2.zero;
         float radius = 4f;
         Vector2 last$$anonymous$$ousePosition = Vector2.zero;
     private void Update() {
 
         float distance = Vector2.Distance(Camera.main.ScreenToWorldPoint(Input.mousePosition),PlayerPosition);
 
         if(distance < radius)
         {
             last$$anonymous$$ousePosition = Input.mousePosition;
         }
          
         else if (distance >= radius)
         {
             HardwareCursor.SetPosition(last$$anonymous$$ousePosition); 
         }
         
     }

but as this is not a free asset i will try to give more suggestion to solve this without it.

you can check system.windows.form.cursor.position but please note that you will need to add "windows.form.dll" to your directory, but this will work only on windows standalone build, for mobile,web,apple or linux platform, you need to dig your way trough your self because i dont know ho to do it too.

,

This was a good question and I hope this answer helps, if this answer helps please consider accepting it as answer, this way anyone else who looking for same question can easily find this answer.

avatar image MrMiyagi NorthStar79 · Dec 06, 2017 at 11:56 AM 0
Share

Thanks! I've update the question. I don't have to use the mouse cursor. The solution can also use a GameObject that follows the mouse input. Not sure if this changes your answer

avatar image NorthStar79 MrMiyagi · Dec 06, 2017 at 01:42 PM 0
Share

that makes things easier :)

here the code for it.

 Vector2 PlayerPosition = Vector2.zero;
          float radius = 4f;
          Vector2 last$$anonymous$$ousePosition = Vector2.zero;
 
  public GameObject fakeCursor; //we defined a fake cursor sprite that follows invisible real cursor
 
 void Start()
 {
 //Set Cursor to not be visible
 Cursor.visible = false;
 }
      private void Update() {
  
          float distance = Vector2.Distance(Camera.main.ScreenToWorldPoint(Input.mousePosition),PlayerPosition);
  
          if(distance < radius)
          {
 fakeCursor.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
              last$$anonymous$$ousePosition = Input.mousePosition;
          }
           
          else if (distance >= radius)
          {
              fakeCursor.transform.position = Camera.main.ScreenToWorldPoint(last$$anonymous$$ousePosition );
          }
          
      }

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Frickinlaser · Dec 06, 2017 at 01:45 PM

I did this recently except for not limiting to the inner circle around the player.

I put the crosshair as a child to the player in the scene hierarchy and a script on it that updates its position.

The localPosition of the crosshair transform is the vector relative to the player object. So just add the input to localPosition and then clamp the magnitude to the radius.

 var input = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
 var vectorFromParent = transform.localPosition + new Vector3(input.x, input.y, 0) * _speed * Time.deltaTime;
 transform.localPosition = Vector2.ClampMagnitude(vectorFromParent, _radius);



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 HowDoIUnity · Mar 15, 2021 at 08:57 PM 0
Share

I know its been 4 years, but this was insanely helpfull for me! Thanks!

avatar image
0

Answer by Yeezyy · Dec 06, 2017 at 11:31 AM

---Not tested--- What you can try is to use a sprite as your mouse and make it follow mouse input only when if distance between mouse position and player position minus inner circle radius in screenspace smaller than the outer circle radius - inner circle radius and greater than inner circle radius


Use Cursor.visible = false ; to hide hardware default cursor Only supports hardware cursors on macOS, Windows and Linux

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

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

82 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 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 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 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

Huh click and go script working 50% 0 Answers

JavaScript: _player.cursor(280) 1 Answer

Changing cursor over multiple objects 0 Answers

[SOLVED] Mouse cursor disappears in built application 3 Answers

my gun wont follow my mouse 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