Well im kind of new to unity and im making a GTA Styled game i already have the play and the guns i already scripted those. Now im having trouble scripting the Police officer. If someone can help me and tell me a better script than the one i have.
var distance;
var target : Transform;
var lookAtDistance = 15.0;
var attackRange = 10.0;
var moveSpeed = 5.0;
var damping = 6.0;
private var isItAttacking = false;
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
if(distance < lookAtDistance)
{
isItAttacking = false;
renderer.material.color = Color.yellow;
lookAt ();
}
if(distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
if(distance < attackRange)
{
attack ();
}
if(isItAttacking)
{
renderer.material.color = Color.red;
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
Just a little note, using Vector3.Distance for checking distance means you are performing the check on each frame. You could actually get rid of this calculation using a trigger.
Add a sphere collider to your cop and add a wanted boolean to your player that is set to true when he shoots in the street or beat up/kill someone
var target :PlayerScript;
var chase :boolean;
function Start(){
target = GameObject.Find("Player").GetComponent(PlayerScript);
}
function OnTriggerEnter(other:Collider){
if(other.gameObject.tag=="Player" && target.wanted)
chase = true;
}
Now you are only checking if the boolean is true.
function Update(){
if(chase){
//everything else
}
}