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 kevinspawner · Jan 29, 2015 at 07:50 AM · vector3floatmathfclamp

How to limit the distance to Half of the Circle from its actual raidus

How to limit the distance to half circle. I want to know, how to clamp the position to half the circle. Currently, in my code am able to clamp it to a circle. But I want the clamping to be only on half of its total radius. How do I do it?

Note: I have not pasted the complete code. If in case you need the complete code (am quiet sure that is not needed for this purpose), I can paste it.

Please help me to solve this issue. I need the answer in c# please. Thanks.

                             throwMovPos =  Camera.main.ScreenToWorldPoint(touch.position);    
                             temp = Mathf.Clamp((Vector3.Distance(throwMovPos,throwStartPos )),-0.4f,0.4f );
 
                             differenceVector = (Vector3.Normalize(throwStartPos - throwMovPos))*temp;
                             difference = temp;



alt text

attachment.png (31.0 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
1
Best Answer

Answer by taxvi · Jan 29, 2015 at 08:01 AM

 temp = Mathf.Clamp((Vector3.Distance(throwMovPos,throwStartPos )),-0.2f,0.2f );
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 kevinspawner · Jan 29, 2015 at 08:10 AM 0
Share

-0.2f,0.2f, This is the radius of the circle. Not sure why you dropped it from 0.4 to 0.2? How would it help to clamp in only half? It will simply reduced the size of circle!

avatar image taxvi · Jan 29, 2015 at 08:39 AM 0
Share

isn't that what you want? or if you want to have your temp variable intact use this:

 differenceVector = (Vector3.Normalize(throwStartPos - throw$$anonymous$$ovPos))*$$anonymous$$athf.Clamp(temp, -0.2f, 0.2f);
avatar image taxvi · Jan 29, 2015 at 08:40 AM 0
Share

you never said that temp variable also controls the radius of the circle

avatar image taxvi · Jan 29, 2015 at 08:42 AM 0
Share

oh, ok, I saw the edit now, wait a sec

avatar image taxvi · Jan 29, 2015 at 08:45 AM 0
Share
  throw$$anonymous$$ovPos =  Camera.main.ScreenToWorldPoint(touch.position);    
  if(throw$$anonymous$$ovPos.x > throwStartPos.x){ // you may need to use < ins$$anonymous$$d
      temp = $$anonymous$$athf.Clamp((Vector3.Distance(throw$$anonymous$$ovPos,throwStartPos )),-0.4f,0.4f );
  }
  else{
      temp = 0f;
  }
avatar image
1

Answer by GameVortex · Jan 29, 2015 at 08:43 AM

The distance to something is never negative (what would a negative distance be?). If -0.4 to 0.4 is your circle, does it mean that the radius of your circle is 0.8 in total? In that case you need to clamp the distance between 0.0 and 0.4 (half the radius).

  temp = Mathf.Clamp((Vector3.Distance(throwMovPos,throwStartPos )),0.0f,0.4f );
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 GameVortex · Jan 29, 2015 at 08:50 AM 0
Share

Aha. Saw your edit now as well. You are saying in the question that you want to clamp to half of the radius but you actually mean half of the circles Radians. You can easily do that by clamping the X value of the differenceVector. If the x value is positive it means the vector is pointing right (right side of circle), if it is negative it will be pointing left (left side of circle).

Basically what @taxvi does as well.

avatar image kevinspawner · Jan 29, 2015 at 08:52 AM 0
Share

Thanks for your replies. Sorry, my explanation was not good enough. Alright let me break it down. This script is for a touch position. I want the user, to touch and drag inside a radius[Circle] 0.4. But I want to limit it to only half the circle as indicated in my image. Please check the image above.

avatar image kevinspawner · Jan 29, 2015 at 08:55 AM 0
Share

Thanks Taxvi and Game vortex.I guess some weird problem with updating my replies. For some reason it gets updated late.

avatar image GameVortex · Jan 29, 2015 at 08:59 AM 0
Share

Not really. Why do you not want to use if else statement? You can use $$anonymous$$athf.Clamp to clamp down the X value, but if you think that $$anonymous$$athf.Clamp is not an if else statement then you are unfortunately very wrong.

avatar image kevinspawner · Jan 29, 2015 at 09:19 AM 0
Share

Thanks. I will use the $$anonymous$$ath F to clamp the values. Thanks a lot for your help. $$anonymous$$uch appreciated.

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

20 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

Related Questions

How to make Vector3s communicate with Floats? 2 Answers

Mathf.Lerp float between 0 and 1 based on boolean input 3 Answers

Precision problem when moving Rect values 2 Answers

Math question 1 Answer

Direction and Distance from One Object to Another 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