- Home /
Make AI object with Nav Mesh work with script to stop following player?
Hello,
I am relatively new to coding and am trying to make this AI script work with navmesh so that an object 'Enemy' does the following;
1) looks at the player within a certain distance
2) Moves towards the player within a certain
3) once it is doing 2 and reaches 1.5 away from the player it stops
4) it continuously follows the player until the player gets a certain distance away from it
5) it moves around objects -
I did have 1 to 4 working properly but since ive added the Nav Mesh part to my script the Enemy completes 1 and 2 and 5 but not 3 or 4, it just follows the player continuously. Here is my script for the enemy object, the 'function chase' is the part I changed in order for the Nav Mesh thing to work. Note the lines I commented out:
var Distance;
var Target : Transform;
var lookAtDistance = 25.0;
var chaseRange = 15.0;
var attackRange = 1.5;
var moveSpeed = 5.0;
var Damping = 6.0;
var attackRepeatTime = 1;
var TheDamage = 40;
private var attackTime : float;
var controller : CharacterController;
var gravity : float = 20.0;
private var MoveDirection : Vector3 = Vector3.zero;
function Start ()
{
attackTime = Time.time;
}
function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
lookAt();
}
if (Distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
if (Distance < attackRange)
{
attack();
}
else if (Distance < chaseRange)
{
chase ();
}
}
function lookAt ()
{
renderer.material.color = Color.yellow;
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function chase ()
{
renderer.material.color = Color.red;
//moveDirection = transform.forward;
//moveDirection *= moveSpeed;
GetComponent(NavMeshAgent).destination = Target.position;
// moveDirection.y -= gravity * Time.deltaTime;
//controller.Move(moveDirection * Time.deltaTime);
}
function attack ()
{
if (Time.time > attackTime)
{
Target.SendMessage("ApplyDamage", TheDamage);
Debug.Log("The Enemy Has Attacked");
attackTime = Time.time + attackRepeatTime;
}
}
function ApplyDamage ()
{
chaseRange += 30;
moveSpeed += 2;
lookAtDistance += 40;
}
If there is any lack of clarity feel free to ask, thanks.
ah, i had something close to this, my script is much different than yours but $$anonymous$$e took advantage of the Distance function.
var ObjectName : String; // name of the object the enemy will follow.
var target : Transform; // this is the enemy.
private var myTransform : Transform; // this is the auto filled when the game starts. ( no need to fill out anything there.
var moveSpeed = 3; //how fast the enemy will move, ( if you have Nav$$anonymous$$esh agent, ingor this.
var rotationSpeed = 3; //how fast the enemy turns ( doesn't seem to work.
var Distance : int; // this is what you want. ( this is the distance you'll set to engage and disengage the player.
function Update(){
Walk();
EnemyStop();
}
function Walk(){ // follows player when in range.
if ( Vector3.Distance(target.position, transform.position ) < Distance){
transform.LookAt(Vector3(target.position.x, transform.position.y, target.position.z));
gameObject.GetComponent(Nav$$anonymous$$eshAgent).enabled = true;
gameObject.GetComponent(Nav$$anonymous$$eshAgent).destination = target.position;
}
}
function EnemyStop(){ // Stops the enemy when Player is out of range.
if ( Vector3.Distance(target.position, transform.position ) > Distance){
gameObject.GetComponent(Nav$$anonymous$$eshAgent).enabled = false; // now this is line not how i stopped my characters (mess around with it to see what works, and if anyone else has better ways than $$anonymous$$e, go with theirs).
}
}
Answer by ohlin · Nov 18, 2014 at 10:47 AM
var target : Transform; //drag player here in inspector
var Distance;
var chaseDistance : float = 10.0;
var closeStopDistance : float = 2.0;
var agent: NavMeshAgent; //drag navmesh enemy here in inspector
function Start(){
}
function Update()
{
Distance = Vector3.Distance(target.position, transform.position);
if (Distance < chaseDistance){
agent.SetDestination(target.position);
}
if (Distance < closeStopDistance){
agent.Stop(false);
}
//etc... just fill in all the distance based actions...
// if (enemyAgro = 10 && Distance < 3.0) then attackfunction() etc.
}
thanks, this helps with some other things i was wondering about.
also looking over what i wrote my script was kinda obsolete after i reworked my enemy npcs and such, to work with animators ins$$anonymous$$d of individual models.
Your answer
Follow this Question
Related Questions
How to make enemy AI with NavMesh 2 Answers
Navmesh Trouble 1 Answer
How do I limit the path of a NavMeshAgent? 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Unity 3D Audio Clip Disabled 1 Answer