Hi all,
I have a question regarding calculating points on a semi circle, with increasing radius. I managed to create a semi circle from X amount of points (in this case 8), but the point don’t start on the right place (the whole thing needs to rotate a certain angle, starting from the npc in random direction).
Here is the code and some explanation:
It uses a random direction (clockwise or counterclockwise) by randomly using +1 or -1.
radius for the npc->player is the initial radius, and stepwise (radius increment) this should increases up to the fleedistance radius.
direction = 1 * (Random.Range(0, 2) * 2 - 1);
int amountOfPoints = 8;
Vector3 playerPos = player.transform.position;
Vector3 npcPos = npc.transform.position;
Vector3 playerPosProj = new Vector3(playerPos.x, npcPos.y, playerPos.z); //only consider x and z axis, it should all take place on the ground.
float radius = Vector3.Distance(playerPosProj, npcPos);
float fleedistance = NPCProperties.fleeDistance;
for (int i = 0; i < amountOfPoints; i++)
{
//logic to calculate the specific points.
Vector3 dirToPlayer = npc.transform.position - player.transform.position;
float correctionAngle = Vector3.Angle(dirToPlayer, player.position);
float angle = 180 / amountOfPoints * i + (direction * correctionAngle);
//radius should be gradually calculated. From beginning it should start with the distance player->npc and increase up to the fleedistance (+correction).
//last i should have multiply of fleedistance+radius, first should have 1/amountOfPoints th of the fleedistance added to the current distance.
float radiusIncrement = fleedistance * ((i / amountOfPoints));
float incrementingRadius = radius + radiusIncrement;
//float randomDirection = either -1 or +1;
float radian = Mathf.Deg2Rad * angle;
float x = incrementingRadius * Mathf.Cos(radian) + incrementingRadius * Mathf.Sin(radian) + playerPosProj.x;
float z = -incrementingRadius * Mathf.Sin(radian) + incrementingRadius * Mathf.Cos(radian) + playerPosProj.z;
Vector3 subpoint = new Vector3(x, npcPos.y, z);
//Creates an object on the place for debugging
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = subpoint;
sphere.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
points.Add(subpoint);
}