Hi i’m making a small AI that will be able to see and run from you in a contained area ( i don’t want him to be able to run from you across the whole level)
So I made a bounding area in code for the enemy and decided to make it so that if the next step ment that the bounds did not contain the enemy then rotate and head back to the center of the bounds.
My problem is sometimes it doesn’t work i played with it for a few minutes and all was fine then he was able to get out of his bounding area and wont rotate back to wards the center.
This is the code:
function RotateTowardsCenter (rotateSpeed : float) : float
{
var bounds = Bounds (enemyRangeCenter, enemyRange);
var relative = transform.InverseTransformPoint(bounds.center);
var angle = Mathf.Atan2 (relative.x, relative.z) * Mathf.Rad2Deg;
// Clamp it with the max rotation speed
var maxRotation = rotateSpeed * Time.deltaTime;
var clampedAngle = Mathf.Clamp(angle, -maxRotation, maxRotation);
// Rotate
transform.Rotate(0, clampedAngle, 0);
// Return the current angle
return angle;
}
Any thoughts?