Quantcast
Channel: Questions in topic: "follow"
Viewing all articles
Browse latest Browse all 536

NavMesh AI: follow only if seen

$
0
0
Hello everyone, As the title says, I want to know how can an enemy with NavMesh AI follow ONLY if it sees the player? Here is the script for the AI: var speed : float = 10.0; var player : Transform; private var dir : Vector3; private var dirFull : Vector3; function FixedUpdate() { dir = (player.position - transform.position).normalized; var hit : RaycastHit; if (Physics.Raycast(transform.position, transform.forward, hit, 1)) // 20 is raycast distance { if (hit.transform != this.transform) { Debug.DrawLine (transform.position, hit.point, Color.white); dir += hit.normal * 20; // 20 is force to repel by } } // more raycasts var leftRay = transform.position + Vector3(-0.125, 0, 0); var rightRay = transform.position + Vector3(0.125, 0, 0); if (Physics.Raycast(leftRay, transform.forward, hit, 1)) // 20 is raycast distance { if (hit.transform != this.transform) { Debug.DrawLine (leftRay, hit.point, Color.red); dir += hit.normal * 20; // 20 is force to repel by } } // check for rightRay raycast if (Physics.Raycast(rightRay, transform.forward, hit, 1)) // 20 is raycast distance { if (hit.transform != this.transform) { Debug.DrawLine (rightRay, hit.point, Color.green); dir += hit.normal * 20; // 20 is force to repel by } } var rot = Quaternion.LookRotation (dir); transform.rotation = Quaternion.Slerp (transform.rotation, rot, Time.deltaTime); transform.position += transform.forward * (2 * Time.deltaTime); // 20 is speed }

Viewing all articles
Browse latest Browse all 536

Latest Images

Trending Articles



Latest Images