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
2
Question by FreeTimeDev · Jun 14, 2012 at 04:49 PM · mousecircleradius

Limit Mouse Look to Circle

Prefacing: I'm creating a 2.5d Side Scroller. I'm beginning to program a cursor. It's a custom cursor (I hide the cursor using Screen.showCursor = false). I'm trying very hard to limit the cursor to a small radius around the player.

Problem: Well, obviously, something isn't working. I've tried using the pseudocode here: http://answers.unity3d.com/questions/42686/keep-object-inside-circle.html and that got me much further than I would have on my own but I'm running into some problems. When I set the radius of the circle absurdly high (5, or so) the "distance" returns to be between -1 and 1. Which makes sense, since it's a % across the screen. However, when I change to be .2 then all of the sudden the distance is .9 or higher. (and my cursor seems to jump to up to "13 screens" away when the distance is 13+)

 #pragma strict
 var regCursor : Texture2D;
 var diff : Vector2;
 var parent : GameObject;
 var circleRadius : float = 5;
 
 function Start(){
 Screen.showCursor = false;
 transform.localScale = Vector3(.03,.03,.03);
 guiTexture.texture = regCursor;
 }
 
 function Update(){
 var mousePos = Input.mousePosition;
 
 MoveMouse(mousePos.x/Screen.width, mousePos.y/Screen.height);
 }
 
 function MoveMouse(mousePosX : float, mousePosY : float) {
 var location = transform.position;
 var parentLocation = Camera.main.WorldToViewportPoint(parent.transform.position);
 
 //Actually Move our Software Mouse
 var parentLoc = new Vector2(parentLocation.x, parentLocation.y);
 diff = (location - parentLoc);
 var distance = (location - parentLoc).magnitude;
 
 Debug.DrawLine(transform.position, parent.transform.position, Color.red, 10.0, true);
 print("dist is "+distance+" circleRadius is "+circleRadius+ ": "+distance+">"+circleRadius);
 if(distance > circleRadius){
     transform.position = parent.transform.position + (diff / distance) * circleRadius; 
     }
 else{
     transform.position.x = mousePosX;//these two lines work alone
     transform.position.y = mousePosY;
 }
 }



The Debug line starts and stays in the same spot. Edit: It does point to somewhere based on where the player is standing and I can't figure out why. For some reason, when I run all the way right, it points up left. Any insight here?

Edit: It's been answered, and here's the result:

 var regCursor : Texture2D;
 var diff : Vector2;
 var parent : GameObject;
 var circleRadius : float = 5;
 var mousePos : Vector3;
 
 function Start(){
 Screen.showCursor = false;
 transform.localScale = Vector3(.03,.03,.03);
 guiTexture.texture = regCursor;
 }
 
 function Update(){
 mousePos = Input.mousePosition;
 MoveMouse(mousePos.x/Screen.width, mousePos.y/Screen.height);
 
 }
 
 function MoveMouse(mousePosX : float, mousePosY : float) {
 var location = Camera.main.ScreenToViewportPoint(mousePos);
 var parentLocation = Camera.main.WorldToViewportPoint(parent.transform.position);
 var parentLoc = new Vector3(parentLocation.x,parentLocation.y,0);
 //Actually Move our Software Mouse
 diff = (location - parentLoc);
 
 var distance = (location - parentLoc).magnitude;
 
 if(distance > circleRadius)
 
     {
 
         transform.position.x = (parentLoc.x +(diff.x/distance) *circleRadius);
         transform.position.y = (parentLoc.y +(diff.y/distance) *circleRadius);
         
     }
     
 else
     {
 
     transform.position.x = mousePosX;
     transform.position.y = mousePosY;
     }
 }

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
Best Answer

Answer by whydoidoit · Jun 14, 2012 at 05:45 PM

Well you are converting parentLocation to viewport coordinates but location is still in world coordinates - that's got to be the root of your problem.

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 FreeTimeDev · Jun 15, 2012 at 06:33 AM 0
Share

I actually messed up in a lot of ways. But, your answer lead me to what I needed to see ins$$anonymous$$d of giving me what I wanted to read. I'm updating my post with the my final code.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Drawing Sector around GameObject in GameView: GUITexture or 3D Object 1 Answer

Rotating around a point in a circular motion depending on mouse position 1 Answer

Find distance around a circle, from center point and another point 2 Answers

Getting radius around multiple transform 1 Answer

Rotate around a circle 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