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 /
avatar image
0
Question by NaZer · Nov 12, 2012 at 08:24 PM · movementmousecircleconstrain

Mouse Controlled object moving inside a circle.

Ok so im trying to make an object that is controlled by the mouse but is limited to a circle. the purple dot is the object and as you can se it still needs to move even though the mouse exits the circle (the mouse should not be visible in-game) And also the whole thing needs to be a child of another object so it moves relative to its parents position. But so far ive had no luck at achieving this.

alt text

I hope you understood my problem and are able to help me Thank you :)

unity problem 2.jpg (44.7 kB)
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

2 Replies

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

Answer by Scribe · Nov 12, 2012 at 09:13 PM

I wrote this:

 var Parent : Transform;
 var Obj : Transform;
 var Radius = 5;
 var Dist : float;
 var MousePos : Vector3;
 var ScreenMouse : Vector3;
 var MouseOffset : Vector3;
 
 function Update() {
     MousePos = Input.mousePosition;
     ScreenMouse = camera.main.ScreenToWorldPoint(Vector3(MousePos.x, MousePos.y, Obj.position.z-camera.main.transform.position.z));
     MouseOffset = ScreenMouse - Parent.position;
     Obj.position.x = ScreenMouse.x;
     Obj.position.y = ScreenMouse.y;
     
     Dist = Vector2.Distance(Vector2(Obj.position.x, Obj.position.y), Vector2(Parent.position.x, Parent.position.y));
     
     if(Dist > Radius){
         var norm = MouseOffset.normalized;
         Obj.position.x = norm.x*Radius + Parent.position.x;
         Obj.position.y = norm.y*Radius + Parent.position.y;
     }
 }

However Jamie suggested much the same thing and got in there first! Others might find this useful in the future however so I thought I'd share it.

Also might help if you can't work out how to utilise Jamie's code.

Scribe

Comment
Add comment · Show 5 · 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 NaZer · Nov 12, 2012 at 09:48 PM 0
Share

I can't seem to get it fully working the object is moving as it should almost my only problem is that as soon i test it the axis point moves to the very bottom. And also the object doesnt follow when the parent moves.

avatar image Scribe · Nov 12, 2012 at 10:04 PM 0
Share

I don't quite understand what you mean by axis point, however I have fixed the parent movement problem. If you can explain the other problem a little more I can try to fix that too. It could be as I am only changing the x and y position of the objects? what axis are you using, from what view is your camera pointing?

avatar image NaZer · Nov 12, 2012 at 10:33 PM 0
Share

It was the exact same thing i was working with (the parent.position) but the problem is that there is a small offset between the parent and the object on the z axis, so when you plus the parents position it moves away in the z axis.

and i keep getting this error: "Operator '+' cannot be used with a left hand side of type 'UnityEngine.Vector3' and a right hand side of type 'float'." I dont have enough basic knowledge of how this works to fix it.

avatar image Scribe · Nov 13, 2012 at 05:56 PM 0
Share

Sorry about that, changed some of the code in the if statement so it should not be effected at all by the z position. "Operator '+' cannot be used with a left hand side of type 'UnityEngine.Vector3' and a right hand side of type 'float'." is pretty much what it says, you can't write Vector3(a, b, c) + d... it doesn't know what to add d to. Though I pressume this came up whilst you were trying to fix your code as there should not be any errors in the above code now.

Found some weird glitches which were a result of me forgetting to find an offset of the mouse position from the parent. fixed that now aswell!

Goodluck, hopefully it all works now, Scribe

avatar image NaZer · Nov 13, 2012 at 08:41 PM 0
Share

Thanks alot, this did the trick!

avatar image
2

Answer by JamieFristrom · Nov 12, 2012 at 08:54 PM

It depends a lot on what you're using to move your purple dot, but after the move happens you can constrain it within the circle.

(This is pseudocode - rewriting in js or csharp up to you.)

In world space:

 if( Vector3.Distance( position, origin ) > maxDistance )
   newPosition = (position-origin).normalized * maxDistance + origin;

Or, if your purple dot is parented to the center, might look something like this:

 if( position.magnitude > maxDistance )
       newPosition = position.normalized * maxDistance;

(Also, I recommend boning up on your vector math if you're going to be making a 2D game - time spent now will save you time later.)

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 NaZer · Nov 12, 2012 at 09:46 PM 0
Share

Sure, do you have any recommendations of where to learn about this? Edit: just one thing to take into account, and i feel stupid not having mentioned it. It's '2D' as in its seen from the side but everything is modelled in 3D to get some depth.

avatar image JamieFristrom · Nov 12, 2012 at 10:22 PM 0
Share

Essential $$anonymous$$athematics for Games is a good one.

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

11 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

Related Questions

Moving a game piece with the mouse 1 Answer

Dragging movement Speed 1 Answer

Move player in circle around center 0 Answers

Runescape Movement and camera rotation 2 Answers

move Game Object(hand) with mouse movement 2 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