- Home /
Question by
JackoMiG · Jun 04, 2014 at 05:17 PM ·
ainavmeshnavmeshagentcomponent
How to add variables to Components (Nav Mesh Agent)?
Is it possible to add variables to the nav mesh data component. I want my enemy ai to stop at a certain distance from the player but it seems it ignores the rest of my script when I activate the nav mesh agent in my script. Here's the script (note i have commented out the 'myagent' stuff out as I thought it would work but it didn't. I also commented out the movedirection stuff in the function chase so the nav mesh component would work[sorta]):
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;
//private var myagent;
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)
{
animation.CrossFade("idle");
}
if (Distance < attackRange)
{
attack();
}
else if (Distance < chaseRange)
{
chase ();
}
}
function lookAt ()
{
animation.CrossFade("idle");
//myagent.enabled = !myagent.enabled;
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
function chase ()
{
animation.CrossFade("run");
//moveDirection = transform.forward;
//moveDirection *= moveSpeed;
//myagent = GetComponent(NavMeshAgent).destination = Target.position;
GetComponent(NavMeshAgent).destination = Target.position;
//moveDirection.y -= gravity * Time.deltaTime;
//controller.Move(moveDirection * Time.deltaTime);
}
function attack ()
{
if (Time.time > attackTime)
{
//myagent.enabled = !myagent.enabled;
animation.CrossFade("attack01");
Target.SendMessage("ApplyDamage", TheDamage);
Debug.Log("The Enemy Has Attacked");
attackTime = Time.time + attackRepeatTime;
}
}
function ApplyDamage ()
{
chaseRange += 30;
moveSpeed += 2;
lookAtDistance += 40;
}
Comment
Your answer
Follow this Question
Related Questions
How do I limit the path of a NavMeshAgent? 0 Answers
Allow navmesh agents to be pushed off navmesh 1 Answer
How can I make the NavMeshAgent travel only in straight lines? 1 Answer
Editing NavMesh Paths? 0 Answers
Change Navmesh Agent AI Outputs? 0 Answers