Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CetraGames · Aug 03, 2012 at 08:00 AM · aienemyfor

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); }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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
    }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
  }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Aug 03, 2012 at 08:30 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges