- Home /
NEED HELP WITH ENEMY AI SCRIPT
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); }
Answer by fafase · Aug 03, 2012 at 08:36 AM
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
}
}
Answer by Griffo · Aug 03, 2012 at 08:17 AM
This might help you, you are missing the functions, this is a script I used a while ago with just a cube to do some testing.
var attackSpeed = 3.0;
var dampingLook = 6.0;
var dampingRun = 1.0;
var startOfRun : Transform;
var endOfRun : Transform;
var speedToRun = 1.0;
var CatSound : AudioClip;
var catAnimateCntrl : AnimationControllerCat; // Set link to my AnimationControllerCat script that is attached to the Cat
private var isColliding : boolean;
function Awake()
{
if(!Target)
{
Target = GameObject.FindWithTag("Player").transform;
}
}
function Update()
{
distance = Vector3.Distance(Target.position, transform.position);
if(canAtackTarget())
{
var targetRotation = Quaternion.LookRotation(Target.position - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * dampingLook);
}
// runs away
if (isColliding == true)
{
catRunOff();
}
}
function canAtackTarget()
{
if(distance > lookAtDistance)
{
renderer.material.color = Color.green;
//print("Out of range");
return false;
}
if(distance < attackRange)
{
renderer.material.color = Color.red;
//print("Attacking");
attack();
return true;
}
var seeYou : RaycastHit;
// Check to see if there is anything between Cat and Mouse
if(Physics.Linecast(transform.position, Target.position, seeYou))
{
if(seeYou.collider.gameObject.tag != "Player")
{
//print("Can not see you " + seeYou.collider.gameObject.name + " in the way");
renderer.material.color = Color.green;
return false;
}
else
{
//Player ditected
renderer.material.color = Color.yellow;
catAnimateCntrl.Idel();
return true;
}
}
return true;
}
function lookAt()
{
var rotation = Quaternion.LookRotation(Target.position - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
animation.CrossFade("Idel",0.2); // Play the Idel animation
}
function attack()
{
// The Vector3 0.2 stops the object going into the floor
transform.Translate(Vector3(0, 0.2, 1) * attackSpeed *Time.deltaTime);
catAnimateCntrl.Walk();
}
// rotate Cat to look at waypoint and run off
function catRunOff()
{
var rotation = Quaternion.LookRotation(endOfRun.position - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingRun);
catAnimateCntrl.Run();
transform.position = Vector3.Lerp(startOfRun.position, endOfRun.position, attackSpeed / 250);
}
// Check if cat and mouse collide
function OnCollisionEnter(theCollision : Collision)
{
if((theCollision.gameObject.name == "Player") || (theCollision.gameObject.name == "Weapon"))
{
isColliding = true;
audio.pitch = Random.Range(0.8f, 1.0f);
audio.PlayOneShot(CatSound);
return;
}else{
isColliding = false;
}
}
I'm confused, you have a pack of undeclared variables starting with Target following right away with distance.
You could easily optimize some parts like placing and else if in the canAtackTarget or the collision function does not need the else.
Your answer
Follow this Question
Related Questions
2D enemy Field of Vision script 1 Answer
AI script troubles 1 Answer
How do I animate my enemy randomly with time? 1 Answer
C# AI, Enemy is flying at me when I jump. 2 Answers
How can an enemy deplete players health? 0 Answers