- Home /
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;
Answer by taxvi · Jan 29, 2015 at 08:01 AM
temp = Mathf.Clamp((Vector3.Distance(throwMovPos,throwStartPos )),-0.2f,0.2f );
-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!
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);
you never said that temp variable also controls the radius of the circle
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;
}
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 );
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.
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.
Thanks Taxvi and Game vortex.I guess some weird problem with updating my replies. For some reason it gets updated late.
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.
Thanks. I will use the $$anonymous$$ath F to clamp the values. Thanks a lot for your help. $$anonymous$$uch appreciated.
Your answer
Follow this Question
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