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 24, 2016 at 11:48 PM · scripting problemraycastaishootingenemy ai

Raycast Enemy AI shooting script

Okay, so these are the stats. I'm trying to make a third person shooter. I have created two prefab bullets. One is shot by my player. Another is used by my enemies. The idea is for them (the enemies) to move around (chase after me), and shoot at me. I don't want them to fill me with bullets right when the game starts and to continue firing without pause. There should be a fire rate limiting that. I had a script that worked badly. Now I have another one but the enemies just stand around doing nothing at all. Can you please help me to fix it? Thanks! Here's the script:

 var fwd : Vector3;    //raycast
  var distance : float = 10.0f;                    //distance
  var timer : float;
  var amountOfBullets : int = 0;
  var amountOfBullets_reset;
  var bullets_Array : GameObject[];
  var hit : UnityEngine.Collision; 
 
  function Start() {
   fwd = transform.TransformDirection(Vector3.forward);
      }
  
       
   function amountofBullets_reset(){
       bullets_Array = GameObject.FindGameObjectsWithTag("Bullet");
       
       amountOfBullets = bullets_Array.Length;
   }
  
   
  function Update () 
   {
       if (Physics.Raycast (transform.position, fwd, distance)) {    //raycast forward and see...
       if(hit.transform.gameObject.name == "MainCharacter"){        //whether it is a player...
           Fire();                        //if this runs, then the player is in the sight of the enemy! 
       }
   }
  }
  
  function Fire () { 
  
  //inside of firing function:
   if(timer > Random.Range(3,5)&&amountOfBullets<5){
       Fire();
       timer = 0.0f;
   }
   timer += 1 * Time.deltaTime;
   amountOfBullets_reset(); 
  
   }
    

@ZefanS @LucasMars You guys have been very wise and helped me greatly in the past. Do you think you could please help me now too? Thanks!

Comment
Add comment · Show 1
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 LucasMars · Mar 24, 2016 at 11:53 PM 0
Share

Are the bullets being destroyed when entering the player? If not, then the bullets are just increasing in numbers, and when it gets to 5, it cannot create any more. I know you have a script to create blood when the player is hit, but does that blood have a tag of "Bullet"? If so, remove that as the tag.

That should fix it. If it doesn't, comment back and I'll see what else the bug could be.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Rob2309 · Mar 25, 2016 at 02:43 AM

Your problem might be in your fwd vector: you just set it once at startup, which will initialize it to a forward vector at that point in time... instead of using your fwd vector, you might just use transform.forward which will always point to the direction your transform is looking at... :)

Comment
Add comment · Show 5 · 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 LucasMars · Mar 25, 2016 at 09:59 AM 0
Share
  • That makes sense as to be the answer. Alternatively from using transform.forward , you could initialize the fwd variable inside of update.

avatar image Rob2309 · Mar 25, 2016 at 03:28 PM 0
Share

yup that would work too :)

avatar image weedmastersr · Mar 29, 2016 at 07:12 PM 0
Share

@Rob2309 @Lucas$$anonymous$$ars @Zefan S

Thank you so much for your input. I have tried this solution but I kept getting other play time errors so I've abandoned that script. I've found another one. This one has no errors but the bullets instantiate too low, the fire rate this too high (constant barrage of bullets) and the characters move while floating around ins$$anonymous$$d of walking (I'm currently using the standard AI Third Person Controller. So, please if you could assist me in fixing it, it would be great: This is the script:

 public var Target : Transform;    
 public var Projectile : Transform;
  
  public var $$anonymous$$aximumLookDistance : float = 15;
  public var $$anonymous$$aximumAttackDistance : float = 10;
  public var FollowSpeed : float = 5;
  public var $$anonymous$$inimumDistanceFromPlayer : float = 2;
  
  public var RotationDamping : float = 2;
  public var $$anonymous$$oveSpeed : float = 1; 
  function Update ()  {
  
      var distance = Vector3.Distance(Target.position, transform.position);
  
      if(distance <= $$anonymous$$aximumLookDistance) {
          LookAtTarget ();
  
          if(distance <= $$anonymous$$aximumAttackDistance)
              AttackAndFollowTarget (distance);
      }   
   }
  
  
  function LookAtTarget () {
      var dir = Target.position - transform.position;
      dir.y = 0;
      var rotation = Quaternion.LookRotation(dir);
      transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * RotationDamping);
  }
  
  
  function AttackAndFollowTarget (distance : float) {
          if(distance > $$anonymous$$inimumDistanceFromPlayer)
          transform.Translate((Target.position - transform.position).normalized * $$anonymous$$oveSpeed * Time.deltaTime);
  
      Instantiate(Projectile, transform.position + (Target.position - transform.position).normalized, Quaternion.LookRotation(Target.position - transform.position));
  }
avatar image LucasMars weedmastersr · Apr 03, 2016 at 03:54 PM 0
Share

For your new script, to fix the bullet being instantiated too low, you do this: var position : Vector3 = transform.position + (Target.position - transform.position).normalized; position.y += 1; //change this to change the height

To make the amount of bullets slow down, you can add an yield WaitForSeconds (time);

Not sure what you mean by the character floating.

avatar image purpl3grape weedmastersr · Apr 23, 2017 at 06:00 AM 0
Share

For this point: "bullets instantiate too low", you'll need to look into 'Object Pooling'

I've a custom script that I can adapt when I get the chance to do so, that may give you some ideas, if nobody else has posted it by then.

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

85 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Interesting Enemy AI issue 2 Answers

Enemy AI for Shooting Game 1 Answer

Need help with physics base enemy AI movement 1 Answer

AI Problem. 0 Answers

Patrolling enemy not shooting at player 2d platformer 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