- Home /
Question by
unity_EbwnDJwQ_B34-A · Jun 14, 2018 at 01:12 AM ·
navmeshnavmeshagentnavigation
Tutorial AI, NavMesh help
I am making an AI for a tutorial segment. It is supposed to stay in front of the player at all times. If the player rotates then the bot will move in front of the player. As of right now it only works if the player is at (0,0,0). If the player moves then rotates the bot paths to wrong points. Does a navmesh only calculate paths relative to (0,0,0) or am I doing something wrong? Any help is appreciated!
private bool isInRange = true;
void moveToFOV()
{
Vector3 ahead = player.transform.forward * ((minDist + maxDist) / 2.0f);
Debug.DrawRay(player.position, ahead);
agent.SetDestination(ahead);
}
bool isInFrontOfPlayer()
{
RaycastHit hit;
Physics.Raycast(player.position, player.transform.forward, out hit);
if (hit.rigidbody == bot)
{
return true;
}
else
return false;
}
void Update () {
agent.speed = speed * Time.deltaTime;
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
Vector3 angleDirection = -direction;
float angle = Vector3.Angle(angleDirection, player.transform.forward);
//set rotation to always look at the player
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), 0.1f);
//moves the bot to always be in the FOV of player
if (!isInRange)
{
moveToFOV();
isInRange = isInFrontOfPlayer();
}
else
{
isInRange = angle < fovAngle;
}
Comment