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 Kabutak · Jul 04, 2015 at 06:34 PM · c#cameramousepositionuser interfacecrosshair

[Solved] Constraining a position within a radius (2D crosshair)

Okay, so I've been going through forums and stuff, and trying many solutions to this problem yet they all fail and I can't for the life of me see why right now. Hopefully I'm overlooking something simple because this code SHOULD work as intended and it isn't.

I have two cameras in my scene, the second one controls the majority of my HUD and other UI elements, since it's a bit cleaner that way. The game is 2D and I am aligning a crosshair between the player and mouse. I can set it to the mouse's position no problem, and can have it be at a point between the two as well. The problem is I want it constrained within a radius around the player. This code SHOULD be doing that, but instead of clamping it within the radius and allowing it to move normally otherwise, it just produces a 'scaled' result. It is hard to explain. The farther my mouse is from the player, the further the cursor moves, but it is relative to the radius that I want to clamp to. I have tried many variations of this code, but to no avail. Hopefully someone can help me because I'm not sure what the problem is at this point, I've been working on it too long.

Update: I've been working on it some more but still no progress. It has nothing to do with using multiple cameras, since I used similar code with only the main camera to actually fire. The bullet velocity scales according to the distance of the mouse from the player as well. Attempting to get an angle between the direction vector calculated here and something like vector3.right or up always gives 180 as well.

                 Vector3 mousePoint = camera.ScreenToWorldPoint(Input.mousePosition);
                 Vector3 temp = Camera.main.WorldToScreenPoint(player.transform.position);
                 Vector3 playerPoint = camera.ScreenToWorldPoint(temp);
 
                 Vector3 target = mousePoint;
                 Vector3 diff = mousePoint - playerPoint;
                 float mouseDist = diff.magnitude;
 
                 if (mouseDist > crossDist)
                 {
                     target = playerPoint + (diff.normalized * crossDist);
                 }
 
                 target.z = crosshair.position.z;
                 crosshair.position = target;

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 _Gkxd · Jul 10, 2015 at 07:05 PM 0
Share

Have you tried using Debug.Log to print out values of vectors and other variables? That might help you narrow the problem down.

avatar image Kabutak · Jul 11, 2015 at 02:52 AM 0
Share

I have printed all of the values during testing, and it shows that the vector was normalized properly, but I can also see the values s$$anonymous$$dily scaling as I move the mouse. A normalized vector should always have a magnitude of 1, so it doesn't make sense that a directional vector is still carrying a scaled weight.

HOWEVER.

I just Zeroed out the z values and that seems to have fixed it. I'm not sure why, seeing as the game it two dimensional. I guess the mouse pointer is considered on a far enough z position in relation to the others that it messed everything up. Go ahead and post an answer so I can mark it solved Nose$$anonymous$$ills. I knew it was something stupid.

1 Reply

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

Answer by NoseKills · Jul 10, 2015 at 05:46 PM

Can't really spot a mistake in the actual logic, unless it's something strange with the first 3 lines.

Only thing that comes to mind is, you could try to zero the z-components in mousePoint/target, playerpoint and diff.

Edit: according to API docs the ScreenToWorldPoint and WorldToScreenPoint set the z-component to "distance in world units from the camera". So the first 3 lines make your source vectors' z values become non-zero, which in turn causes the normalizing of diff produce vectors that have x an y values that scale in relation to that z value.

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 Kabutak · Jul 11, 2015 at 12:11 PM 0
Share

Well that explains that then. Clearly I should sleep more before I try to program. Thanks for the help, I knew it would be some stupid detail.

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

Zooming in on UI image using mouse cursor position 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Why is Input.mousePosition returning wired values using Cinemachine 2D with dynamic following? 0 Answers

MousePosition with Iso camera movement 1 Answer


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