Skip to content

Instantly share code, notes, and snippets.

@penguin55
Last active June 12, 2019 12:31
Show Gist options
  • Save penguin55/50c5f6c9ff3b7ebae971727e298c5aed to your computer and use it in GitHub Desktop.
Save penguin55/50c5f6c9ff3b7ebae971727e298c5aed to your computer and use it in GitHub Desktop.
Translate an Object Inside a Circle with Radius

How To Translate an Object Inside a Circle with Radius

Sorry if my english is bad. I want to share about my work about translating an object inside a circle.

You must know that circle has radius to define how wide the circle.

Let's begin with the simple explanation.

Circle

Circle

distance = redCircle position - centerBlueCircle position

We just want to get the distance between 2 points. So it will same if you count the opposite.

distance = centerBlueCircle position - redCircle position

After you got the distance, you will know that distance is a how far object to center position of circle. Which means we know that if the distance is less than radius, the object is inside in our circle, but other than the object is out from our circle. So we can restrict the movement of object if the object moves out from the circle.

Restrict Move Inside Circle

Code

Explanation

When the object distance to center is larger than radius, then we make restrict move to the object.

posObj is position of object according to center position. We want to get the position according to center position, so the result is not same when we do it the opposite calculation. Because we want to know is the object position in the left side or rigth side or maybe in top side of circle or bottom side of circle. The variable objPos absolutelly has same value with distance but objPos is the real value without absolut the value.

objPos = objectPosition - center

is not same with

objPos = center - objectPosition

objectPosition is the red circle according to the circle picture and center is the center position of blue circle according to circle picture.

After we count the objPos, then we will calculate the position of object when the object is going moves to outside circle.

objPos = objPos * radius / distance

the formula above will always results a position value that has a distance with a center less than equal to radius.

This is the result :

circle

using UnityEngine;
public class Controller : MonoBehaviour
{
public Transform center;
public float radius;
void Update()
{
// Your movement code here
transform.position = TranslateInsideCircle(transform.position, center.position, radius);
}
/// <summary>
/// This method do Translate inside a circle with radius of circle. This method just doing restrict move to object, so it will can't go outside of the circle
/// </summary>
/// <param name="_position"> The position of object which will moving </param>
/// <param name="_center"> The center position of object to move </param>
/// <param name="_radius"> Radius of movement </param>
/// <returns> Return a Vector2 inside of circle radius </returns>
public Vector2 TranslateInsideCircle(Vector2 _position, Vector2 _center, float _radius)
{
float distance = Vector3.Distance(_position, _center);
if (distance >= _radius) //To keep in the inside circle
{
Vector2 fromOriginToObject = _position - _center;
fromOriginToObject *= _radius / distance;
return _center + fromOriginToObject;
}
return _position;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment