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 Catlard · Sep 28, 2011 at 11:06 AM · circle

Unit Circle Equation Question!

Hey there!

So I'm working with a GUI script that needs to allow someone to drag the cursor by holding down the mouse button, but only to a certain distance away from where they started holding down that mouse button. After that point, the cursor should stay in the circle--but at an angle that's appropriate to where the cursor is outside of the circle. It's sort of the way that Angry Birds allows you to drag the bird a certain distance away from the slingshot, but then keeps the bird at the correct angle, even if you're dragging it farther than the slingshot will allow. So, I'm calculating the angle that the cursor is from the center of the circle correctly (i.e. 360 and 0 are when you're directly "above" it in the GUI, and 180 is below, and so on. However, when the cursor attempts to calculate its position, it's moving way too quickly around the circle. I think it has to do with these two lines of code. Do you guys know what I'm doing wrong, calculating the psoitions of the cursor? I realize this question is kind of vague, and perhaps hard to understand. But I've made a video of what's going on, so you can see what I mean...here's the code:

 circleCoordinates.x =  (dragPlaneRadius * (Mathf.Cos(angleFromOrigin)) + xOffset;
 circleCoordinates.y =  (dragPlaneRadius * (Mathf.Sin(angleFromOrigin)) + yOffset;

and here's the video:

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 syclamoth · Sep 28, 2011 at 11:52 AM 0
Share

I'm sure I've answered something similar to this recently...

avatar image Catlard · Sep 28, 2011 at 12:08 PM 0
Share

heh...I wish I knew that anwer!

avatar image syclamoth · Sep 28, 2011 at 12:23 PM 0
Share

Things to remember- don't try to directly change the cursor position, ins$$anonymous$$d you should hide the cursor and draw a GUI.DrawTexture or the like where your corrected cursor should be. The windowing system sometimes gets rather upset if you try to override the cursor! $$anonymous$$y answer may or may not help you- it depends on exactly what the problem is. From the sounds of things, you already have a way of correcting your cursor position, but it's buggy (hence the question). If my answer doesn't help you, can you post the full code for your cursor correction in an edit to your original question? I can help you debug it.

1 Reply

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

Answer by syclamoth · Sep 28, 2011 at 12:19 PM

Ok then. Assuming that you know the true position of the mouse, and the point at which you want the cursor to rotate, first you get the original offset:

 Vector2 originalOffset = mousePosition - rotateAroundThis;

Then you use this handy function called Atan2-

 float angle = Atan2(originalOffset);

Then use that and your desired radius to reconstruct the correct location.

  float correctRadius = Mathf.Clamp(0, desiredRadius, originalOffset.magnitude);

 Vector2 correctOffset = new Vector2(correctRadius * Mathf.Cos(angle), correctRadius * Mathf.Sin(angle));

Then use the correctOffset to draw your cursor!

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 syclamoth · Sep 28, 2011 at 12:20 PM 0
Share

This is in C#, because it's my mother tongue (so to speak). I can translate it into Javascript if you need me to!

avatar image Catlard · Sep 28, 2011 at 12:31 PM 1
Share

Ah! this works perfectly, I've just thrown it in...but I just realized my first problem problem--I was using degrees to calculate something I needed radians for! GAHHHH...thanks for your help.

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

Moving Array Objects in a Ellipse (javascript) 1 Answer

A way to create an invisible circle for the enemy? 4 Answers

How to create a circle around a selected game object? 1 Answer

Create a circle using cubes? 3 Answers

Gaussian Distribution in circle on x and y axis 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