- Home /
keep object inside circle
i have a texture attached to the Input.mousePosition. so when i move mouse around texture follows the mouse. i want to limit the area of movement to the circle. so mouse can go outside the circle but the texture should stick to the border of the circle. here is a image that could help. i guess it is a simple math, but i just cant figure out how to control what is used and what not
any directions should be appreciated:
Comment
Best Answer
Answer by Jesse Anders · Jan 13, 2011 at 04:47 PM
Pseudocode (untested):
Vector2 diff = mousePosition - center;
float distance = diff.magnitude;
if (distance > circleRadius) {
cursorPosition = center + (diff / distance) * circleRadius;
} else {
cursorPosition = mousePosition;
}
Thank you! No vote power but have a virtual beer on me.