Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by weedmastersr · Mar 09, 2016 at 11:31 PM · scripting problemcollisionaishootingenemy ai

Interesting Enemy AI issue

So, I'm making a 3rd person shooter. I have this script for the Enemy AI which is attached to all enemy characters. The problem is they either all shoot at me fast at once, and kill me instantly. Or, they just follow me around and don't shoot at all. It works pretty crappy. Can any of you savvy chaps fix it?

This is it:

  var projectile : Rigidbody;
   
  var speed = 70;
   
  var player : Transform;
  
   var MoveSpeed = 1;
   
   var MaxDist = 20;
   
   var MinDist = 3;
   
   private var timestamp = 0.0;
   
  function Start() {
      var rendum = Random.Range(1F,3F);
      }
   
  function Update () 
   {
       transform.LookAt(player);
       
       if(Vector3.Distance(transform.position,player.position) >= MinDist){
       
            transform.position += transform.forward*MoveSpeed*Time.deltaTime;
   
                       
            if(Vector3.Distance(transform.position,player.position) <= MaxDist  && (Time.time > timestamp))
   
           {clone = Instantiate(projectile, transform.position + new Vector3(0.0f, 1.5f, 0.0f), transform.rotation);
  clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
   
  Destroy (clone.gameObject, 0.5);
  timestamp = Time.time + timestamp;
  } 
  }
  }
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
0
Best Answer

Answer by LucasMars · Mar 10, 2016 at 06:08 PM

The way to get them to not fire all at once is to have a global counter that checks how many bullets have been fired. One way to do this is to have all bullets as a child of another blank gameObject, and then count all children.

Another way to get them to not all fire at once is making them have to aim shots. Give them a Random.Range value to wait, and it'll have to be in that position for a time until it can shoot. That will also probably sort out the overfiring problem. Pseudocode:

 //declared at start:
 var timer : float;
 var amountOfBullets : int = 0;
 
 //inside of firing function:
 if(timer > Random.Range(3,5)&&amountOfBullets<5){
     Fire();
     timer = 0.0f;
 }
 timer += 1 * Time.deltaTime;
 amountOfBullets_reset();

To get it to fire when it is bugging out is also going to need to be fixed. Rather than checking the distance of the object, use a RayCast to see whether the player is in the sight of the enemy. If it is, it can fire. If it isn't, then the player isn't in the sight of the enemy. Pseudocode:

 var fwd = transform.TransformDirection (Vector3.forward);     //raycast
 var distance : float = 10.0f;                    //distance
 
 if (Physics.Raycast (transform.position, fwd, distance)) {    //raycast forward and see...
     if(hit.transform.gameObject.name == "player"){        //whether it is a player...
         Fire();                        //if this runs, then the player is in the sight of the enemy! 
     }
 }


Hope this helps :)

Comment
Add comment · Show 6 · 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 weedmastersr · Mar 10, 2016 at 10:18 PM 0
Share

@Lucas$$anonymous$$ars You sir are my personal hero! :) Thank you so much!

I have replaced my original code with this. The only problem is that I'm getting these two errors. Not sure what's the best way to get around them.

var timer : float; var amountOfBullets : int = 0;

Assets/Scripts/EnemyAI.js(29,2): BCE0005: $$anonymous$$ identifier: 'amountofBullets_reset'.

avatar image LucasMars weedmastersr · Mar 10, 2016 at 10:26 PM 0
Share

You haven't declared any function for amountofBullets_reset(). I put that in as a function, though forgot to explain what the function did.

It would re-find all of the bullet gameObjects. Pseudocode:

 var bullets_Array : gameObject[];
 
 function amountofBullets_reset(){
     bullets_Array = GameObject.FindGameObjectsWithTag("bullet");
     
     amountOfBullets = bullets_Array.Length;
 }

Something like that to test for the amount of bullets currently created.

avatar image weedmastersr LucasMars · Mar 10, 2016 at 11:24 PM 0
Share

@Lucas$$anonymous$$ars Did you mean var bullets_Array : GameObject[]; because if that's the case, I'm getting the same errors. If you actually meant var bullets_Array : gameObject[]; then I'm getting the error: BCE0018: The name 'gameObject' does not denote a valid type ('not found'). Did you mean 'UnityEditor.GameObjectUtility'?

Show more comments
Show more comments
avatar image
0

Answer by weedmastersr · Mar 10, 2016 at 12:50 PM

So sorry to bother you again @LucasMars but nobody else seems to be able to offer any useful advice. How could I improve this script because it works pretty crappy right now?

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

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

77 People are following this question.

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

Related Questions

Enemy AI for Shooting Game 1 Answer

Raycast Enemy AI shooting script 1 Answer

AI Problem. 0 Answers

How To Stop Enemy Movement During Its Attack Animation 1 Answer

Need help with physics base enemy AI movement 1 Answer


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