Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 oliver-jones · Oct 06, 2015 at 04:18 PM · raycasttransformboundsclampcircle

Prevent Object from going out of Circle Radius

Hello,

I'm trying to achieve this:

alt text

I have an object that follows my finger/mouse when I'm within the green area. However, when I drag my finger/mouse into the red area I don't want the object to follow exactly my finger/mouse location anymore, although I do want it to continue to move within the green area. Does that make sense? Almost like a movement cap.

I'm using simple collision raycaster:

 object.transform.position.x = _dragger.hit.point.x;
 object.transform.position.y = _dragger.hit.point.y;

How would I go about preventing the object from going out of the green bound? (The bounds are just circles will collisions).

Thanks.

radius-touch.png (48.2 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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by DiegoSLTS · Oct 06, 2015 at 04:37 PM

I can think of 2 methods:

1 - You know the green circle radius and know the black circle radius. Every time you update the black circle position check if the position plus the black radius is greater than the green radius. If it is, move it inside the green circle in an imaginary line that goes from the finger to the center of the green circle.

2 - Get the position vector relative to the center of the green circle. Normalize that vector and multiply it by "green radius - black radius". Convert that normalized vector back to world position and set it as the position of the black circle.

I think I prefer the second method since it looks more clean, but I haven't tested them, maybe one is faster than the other.

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
avatar image
2

Answer by Jessespike · Oct 06, 2015 at 04:56 PM

Get the vector that is from the center of the circles to your mouse position. Normalize this vector and multiply the max distance (inner circle radius). Position the object to the center and add the pre-calculated direction.

My answer doesn't sound very clear, even to me. Maybe showing in code will clarify things:

 public Transform _Center;       // The very center of the circles
 public Transform _Object;       // Transform of the object that follows mouse
 float maxDistance = 3f;         // Max distance that the object can be from the center
 
  Vector3 screenPointCenter = Camera.main.WorldToScreenPoint(_Center.position);
  _Object.position = _Center.position + (Input.mousePosition - screenPointCenter).normalized * maxDistance;
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 kozakoleg284 · Feb 04 at 09:04 PM 0
Share

u save my life)

avatar image
0

Answer by Pharan · Oct 06, 2015 at 06:38 PM

You can get a direction vector.

// This is the direction of the finger/mouse.
Vector2 direction = fingerMousePosition - centerPosition.
And given the float radius of the circle (slightly smaller than the green circle to specify the center and not the outside); You can set the position of the clamped black circle to:
Vector2 blackCirclePosition = centerPosition + (direction.Normalized * radius);

This logic can be conditional to when your finger/mouse gets too far.

if (direction.Magnitude >= radius) {
// all that stuff here
}
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

31 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

Related Questions

Set forward vector of an object to be EXACTLY equal to another vector. 2 Answers

Want to move object slowly to where the mouse clicks? 1 Answer

Limit the position of the object, but still keep following the touch? How to use ClampMagnitude properly? 1 Answer

How to get the center point of an object without any renderers? 2 Answers

Get closest point on collider on at a certain z depth 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