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
0
Question by arisonu123 · Jul 31, 2017 at 11:56 PM · vector3distancedragdragging

Keep vectors exactly 15 distance apart

So I have a starting vector and I have a vector point with a box graphic that be dragged. Currently it can be dragged up,down, and outwards. I want to constrain it so that it is always 15 distance away as it is dragged around the start point vector. How would I achieve this?

Comment
Add comment · Show 1
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 nt314p · Aug 01, 2017 at 12:19 AM 0
Share

I suggest you use Vector3.Distance and if the distance is greater that 15, restrain that direction of movement. For example, if the box is too high, the script won't let the box move upward, but it will let it move downward.

https://docs.unity3d.com/ScriptReference/Vector3.Distance.html

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by bgprocks · Aug 01, 2017 at 12:39 AM

There is a Vector3.Distance function you can use to check the distance between 2 vectors, you can stop the object from being dragged the moment if is 15 or more from the starting point. you will never stop it at exactly 15, but it will be close enough to fool the human eye

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
0

Answer by Bunny83 · Aug 02, 2017 at 12:35 AM

The general approach is:

  • get the relative vector from your starting / center position to your drag position
    v = P - S;

  • perform the constraint you want on v (see below)

  • get back the absolute vector by doing the reverse of #1:
    P = S + v;

There are different ways you could constrain such a relationship:

  • Always keep an exact distance of 15. This basically restricts the object to only move on a circular path around the center:
    v = v.normalized * 15;

  • Allow any position inside the specifed circle but no position outside the circle.
    v = Vector3.ClampMagnitude(v, 15);

  • Ensure at least a distance of 15 but allow any further position. This would keep the circular area around the center free. So this just ensures a minimum distance,
    if (v.sqrMagnitude < 15*15) v = v.normalized * 15;

So for example the first case would look something like this:

 // always distance of 15 units
 Vector3 v = dragPos - centerPos;
 v = v.normalized * 15;
 dragPos = centerPos + v;

Of course if your "dragPos" comes from a gameobject position you want to update it's position as well.

ps: "ClampMagnitude" basically just does this:

 if (v.sqrMagnitude > 15*15)
     v = v.normalized * 15;
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

83 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 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 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

keeping constant "Distance Joint 2d" 2 Answers

How do i disable my buttons while dragging ScrollRect? 0 Answers

How do I find the farthest verts of a mesh relative to its object position and store them in an array 1 Answer

Dragging object with Mouse causes it to fly away 0 Answers

Find position of point beetween two objects on certain distance 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