Comments and answers for "How to keep an object within a circle/sphere (radius) - NO RECTANGLE"
http://answers.unity.com/questions/1309521/how-to-keep-an-object-within-a-circlesphere-radius.html
The latest comments and answers for the question "How to keep an object within a circle/sphere (radius) - NO RECTANGLE"Comment by Edisyo on Edisyo's answer
http://answers.unity.com/comments/1659148/view.html
Thanks so much!
Only one thing, at least for me, I needed to add
newLocation = centerPosition + fromOriginToObject; //*BlackCenter* + all that Math
//ADDED CODE
transform.position = newLocation;
I use mouse position to update green objects position. At first this didnt work, but I looked at logs and figured out I need to put void, that gets the mouse Input, in Update, not LateUpdate (I red that it needs to be in LateUpdate). Worked perfectly after that!Tue, 20 Aug 2019 13:07:37 GMTEdisyoComment by SheZii on SheZii's answer
http://answers.unity.com/comments/1598422/view.html
Like a charm (y)Tue, 05 Feb 2019 15:08:24 GMTSheZiiComment by CarterG81 on CarterG81's answer
http://answers.unity.com/comments/1311149/view.html
I actually use this for 2D game in a 3D game world. It was to "build structures" in the game; a form of crafting or equivalent of a RTS game where you move your mouse around to place buildings down at specific coordinates.
It works fine in a 3D world, if you are able to handle your Y-Axis (mine was always 0, so no problem here).
I initially needed to convert Mouse Coordinates into 3D world coordinates. However Unity's conversion of Mouse to World coordinates was very jittery. (Moving the mouse wouldn't necessarily move the world coordinates, resulting in huge jumps in world coordinates, no matter what method I tried, including raycasting).
So I ended up just attaching an object to the player, and then attaching a sprite (of what the player is trying to build) to that object (what is in essence a gameobject that acts as a mouse cursor, but for joystick controllers). This "cursor gameobject" moves in the X/Z axis in 3D world space, based on the code I posted.
Eventually I will figure out how to get smooth mouse to world coordinate conversion, and then simply (if not a controller/joystick) move said "cursor gameobject" based on mouse coordinates.Thu, 09 Feb 2017 19:31:27 GMTCarterG81Answer by grizzly48
http://answers.unity.com/answers/1309530/view.html
Hi,
I would simply check the distance.
I´m correct in the assumption, that we´re in the two-dimensional-space?
Vector2 center;
Vector2 position;
float maxDistance;
float actualDistance = Vector2.Distance(center, position);
if (actualDistance > maxDistance)
{
Vector2 centerToPosition = position - center;
centerToPosition.Normalize();
position = center + centerToPosition * maxDistance;
}Tue, 07 Feb 2017 13:33:36 GMTgrizzly48Comment by CarterG81 on CarterG81's answer
http://answers.unity.com/comments/1309531/view.html
Thank you. Your answer was so incredibly quick, I wasn't able to put my own in fast enough. I was trying to document this for others, because I found this question difficult to answer but finally found an answer myself.
I apologize for this. Thank you for this alternative.Mon, 06 Feb 2017 15:21:50 GMTCarterG81Answer by Bunny83
http://answers.unity.com/answers/1309526/view.html
Well, don't use max / min positions as they define a rectangle. What you want is defining a center point and a radius. Then just use [Vector3.ClampMagnitude][1] on the relative vector:
// C#
Vector3 v = newLocation - circleCenter;
v = Vector3.ClampMagnitude(v, circleRadius);
newLocation = circleCenter + v;
If the circle center and your location is not on the same "height" (y value) you can do:
// C#
circleCenter.y = newLocation.y;
Vector3 v = newLocation - circleCenter;
v = Vector3.ClampMagnitude(v, circleRadius);
newLocation = circleCenter + v;
[1]: https://docs.unity3d.com/ScriptReference/Vector3.ClampMagnitude.htmlMon, 06 Feb 2017 15:15:39 GMTBunny83Answer by CarterG81
http://answers.unity.com/answers/1309525/view.html
![Alt text](http://i.imgur.com/VNPoHyX.gif)
float radius = 400; //radius of *black circle*
Vector3 centerPosition = transform.localPosition; //center of *black circle*
float distance = Vector3.Distance(newLocation, centerPosition); //distance from ~green object~ to *black circle*
if (distance > radius) //If the distance is less than the radius, it is already within the circle.
{
Vector3 fromOriginToObject = newLocation - centerPosition; //~GreenPosition~ - *BlackCenter*
fromOriginToObject *= radius / distance; //Multiply by radius //Divide by Distance
newLocation = centerPosition + fromOriginToObject; //*BlackCenter* + all that Math
}
If the distance is < radius, it is within the circle. No need for any calculations.
If the distance is > radius, the object is outside the circle.
1. Determine the center position of the circle/radius. The radius of this circle is also the maximum limit.
2. Determine the distance between point A (circle/bounds) and B (object you want to limit; prevent movement outside of bounds A).
3. If the distance is greater than the radius, then do the following:
4. Get Vector3 **U** from B (object) minus A (circle).
**(U = B - A)**
5. Multiply by Radius
**(U x Radius)**
6. Divide by Distance
**(U / Distance)**
8. Position = U + A **(maxLocation = U + A)**Mon, 06 Feb 2017 15:14:10 GMTCarterG81