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
1
Question by weedmastersr · Mar 29, 2016 at 07:18 PM · javascriptscripting problemaishootingenemy ai

Enemy AI for Shooting Game

Hello everyone,

I'm trying to make a third person shooter. I'm used the standard AI Third Person Controller. I have a prefab for the bullet. And I have a script for the enemy AI. The problem is it works badly. The bullets instantiate too low, the fire rate this too high (constant barrage of bullets) and the characters move while floating around instead of walking. Can you please help me fix it? Here it is:

 public var Target : Transform;    
 public var Projectile : Transform;
  
  public var MaximumLookDistance : float = 15;
  public var MaximumAttackDistance : float = 10;
  public var FollowSpeed : float = 5;
  public var MinimumDistanceFromPlayer : float = 2;
  
  public var RotationDamping : float = 2;
  public var MoveSpeed : float = 1; 
  function Update ()  {
  
      var distance = Vector3.Distance(Target.position, transform.position);
  
      if(distance <= MaximumLookDistance) {
          LookAtTarget ();
  
          if(distance <= MaximumAttackDistance)
              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 > MinimumDistanceFromPlayer)
          transform.Translate((Target.position - transform.position).normalized * MoveSpeed * Time.deltaTime);
  
      Instantiate(Projectile, transform.position + (Target.position - transform.position).normalized, Quaternion.LookRotation(Target.position - transform.position));
  }
Comment
Add comment · Show 2
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 30, 2016 at 09:49 PM 0
Share

@ZefanS You've helped me immensely in the past. Think you could give me a hand with this one too?

avatar image ZefanS weedmastersr · Mar 31, 2016 at 12:29 AM 0
Share

I'll take a look.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by ZefanS · Mar 31, 2016 at 01:56 AM

Okay, so for a basic AI we want, maybe, 3 main abilities: look at the player, shoot at the player, and follow the player.

The first two are relatively simple to set up, and we can do it with essentially the script you already have. I modified it slightly by simply adding a time check in order to reduce the frequency of the enemy shooting. I also removed anything to do with movement.

 #pragma strict
 
 public var target : Transform;    
 public var projectile : Transform;
 
 public var maximumLookDistance : float = 30;
 public var maximumAttackDistance : float = 10;
 public var minimumDistanceFromPlayer : float = 2;
 
 public var rotationDamping : float = 2;
 
 public var shotInterval : float = 0.5;
 
 private var shotTime : float = 0;
 
 function Update()
 {
     var distance = Vector3.Distance(target.position, transform.position);
 
     if(distance <= maximumLookDistance)
     {
         LookAtTarget();
 
         //Check distance and time
         if(distance <= maximumAttackDistance && (Time.time - shotTime) > shotInterval)
         {
             Shoot();
         }
     }   
 }
 
 
 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 Shoot()
 {
     //Reset the time when we shoot
     shotTime = Time.time;
     Instantiate(projectile, transform.position + (target.position - transform.position).normalized, Quaternion.LookRotation(target.position - transform.position));
 }

If you test this out, the enemy should stay still but look and shoot at the player when within the respective ranges.

I removed the movement code from the script because AI movement is something a lot more complicated. Rather than trying to explain everything here, I will provide you with some useful resources to get an idea of what you need to do.

General Theory:

Pathfinding

Don't follow this tutorial, just read it.

What should you choose?

Waypoint Graph or Navigation Mesh?

Okay, so basically what I'm getting at is that you should use Unity's built-in navigation mesh functionality to control your AI's movement and pathfinding.

Here are some resources on that:

From the Manual

Watch out for outdated info, but this is a classic

This video & this video are from a series of some of the best Unity 5 tutorials.

A question you might have

Read all this and understand it. Click around on Wikipedia if there are terms you don't know. This stuff can be complicated, but it's worth understanding if you're going to be working on games with AI. I hope this helps make things clearer.

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 weedmastersr · Mar 31, 2016 at 09:05 AM 0
Share

@ZefanS Thank you!

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

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

Related Questions

Raycast Enemy AI shooting script 1 Answer

Interesting Enemy AI issue 2 Answers

Fix for Enemy AI script 0 Answers

AI Problem. 0 Answers

How To Stop Enemy Movement During Its Attack Animation 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