Find the closest point on a circle's circumference to a point outside the circle.

The game I have has a few people running around on a little circular platform, and shooting at enemies outside the platform. I want them to run to the edge of the platform nearest the enemy before they attack. I have most of the code written, except I can’t find a way to get the point on the platform that the guys need to run to.

I’ve been looking into using Vector3.angle, then running that angle through a function I have to find a point on a circle at any given angle. The problem comes in at Vector3.angle. So if anybody could tell me how to use it, that would be excellent. Basically, I have two points, and I want to find the angle between them relative to world space. So if point A was at (0,1) and B was at (1,1), it would return “90”. If B was at (2,0), it would return 0 or 360.

Thanks in advanced!

try atan or atan2 :wink:

Sure wish you would be a bit more clearer than “try…”

Try this:

Get the position enemy in platforms local space, make that y zero, then multiply that by the radius you have for your platform.

function GetPoint(enemy:Transform, platform:Transform, radius:float){
	var point:Vector3 = platform.InverseTransformPoint(enemy.position);
	point.y = 0;
	return platform.TransformPoint(point.normalized * radius);
}
4 Likes

Very nice.