• Unity
  • Services
  • Made with Unity
  • Learn
  • 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
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

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 seb-lopez · Jan 13, 2017 at 12:49 AM · aienemyaivs

freindly ai attack other ai.

Hi i made a simple ai that shoot only the player. and i was wondering how to make a Ai that actually shoot other ai's (like a huge ai battle). I'm using java, and i know we need to use an array. but i can't see what to do for making the ai attack other ai's. is there a On tags function maybe? i don't really know. can someone help please.

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 jwulf · Jan 13, 2017 at 03:13 AM 0
Share

How do you currently make your ai attack the player? As you have achieved that, what stops you from making it shoot something else than the player?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by seb-lopez · Jan 15, 2017 at 12:22 AM

it's for building friendly ai's to shoot the enemies ai's instead targetting me. i want to make an ai like a sand box. i want to make a script that makes me juste to put multiple ai's in a team and automatically shoot's the other team. a bit like Arma editor where you just place infentries set there teams and see the action taking place. but the problem is that i remarque that there is actually no tutorial explaining that. i know that it's array with loops but i don't know a way to implement that.

here is a turret script for the ai :

 //var player : GameObject[];
 var player : GameObject;
  var safeDist : float = 15;
  var currentDist : float;
  var shooting : boolean = false;
  var bang : AudioClip;
  var bulletFab : Rigidbody;
  var gunTip : Transform;
  var power : float = 1000;
  var shotDelay : float = 1;
 
 
  function Start (){
   player = GameObject.FindGameObjectsWithTag("blueTeam");
  }
  
  
  function Update () {
     
          currentDist = Vector3.Distance(transform.position, player.position);
      
          if(currentDist < safeDist){
              transform.LookAt(transform.position, player.position);
              if(!shooting) shootStuff();
      
          
      }
          
  }
  
  function shootStuff(){
      
      shooting = true;
      
     
      //for ( var i = 0 ; i <player.Length; i++){
          AudioSource.PlayClipAtPoint(bang, transform.position);
      
          var fwd = transform.TransformDirection(Vector3.forward);
          var bulletShot : Rigidbody = Instantiate(bulletFab, gunTip.position, gunTip.rotation);
          bulletShot.AddForce(fwd * power);
      
          yield WaitForSeconds(shotDelay);
          shooting = false;
      
    //  }
  }


what i put in "//" is where i think to put. the problem is that i think that the script was meant for one target.

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 GarretLawrence · Jan 16, 2017 at 07:42 AM

Say each team has 3 AIs : (A1, A2, A3) vs (B1, B2, B3).

Say after getting list of B team, A1 focus fire on B2, only when B2 die (no matter killed by who), A1 will search for other target until no target left. Is that what you want?

If so you should do something like this

 var player : GameObject[];
 var currentTarget : Transform;
 var safeDist : float = 15;
 var currentDist : float;
 var shooting : boolean = false;
 var bang : AudioClip;
 var bulletFab : Rigidbody;
 var gunTip : Transform;
 var power : float = 1000;
 var shotDelay : float = 1;
  
  
 function Start (){
     
 }
   
   
 function Update () {
     //if target is alive/not null then shoot
     if(currentTarget)
     {
         currentDist = Vector3.Distance(transform.position, currentTarget.position);
       
         if(currentDist < safeDist){
             transform.LookAt(transform.position, currentTarget.position);
             if(!shooting) shootStuff();
         }
     }
     else //if not then search for new target
     {
         player = GameObject.FindGameObjectsWithTag("blueTeam");
         //pick random target
         if(player.Length > 0)
         {
             Random rand = new Random();
             var  n = rand.nextInt(player.Length);
             currentTarget = player[n];
         }
     }
 }
   
 function shootStuff(){
       
     shooting = true;
     AudioSource.PlayClipAtPoint(bang, transform.position);
       
     var fwd = transform.TransformDirection(Vector3.forward);
     var bulletShot : Rigidbody = Instantiate(bulletFab, gunTip.position, gunTip.rotation);
     bulletShot.AddForce(fwd * power);
       
     yield WaitForSeconds(shotDelay);
     shooting = false;
 }
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 seb-lopez · Jan 16, 2017 at 09:25 AM 0
Share

even known that this script seems interesting( there is a problem in line 35 with "Random rand" it says that there is no ";" at the end). But i'm going to be more precise by telling like a codition :

is a ai that has a liste of target (in array ). if the target is tag == blue team. and if the ai is near of one of these targets. it will shoot them.

and vise versa

avatar image GarretLawrence · Jan 16, 2017 at 09:35 AM 0
Share

@seb-lopez well then this script does exactly what you want... I'm not sure about how Random method declared in Javascript so maybe i was wrong. You can edit the code to :

          player = GameObject.FindGameObjectsWithTag("blueTeam");
          //pick 1st target from the list
          if(player.Length > 0)
          {
              currentTarget = player[0];
          }
avatar image seb-lopez GarretLawrence · Jan 16, 2017 at 10:46 PM 0
Share

it 's saids :

i never been soo close to achieved this script =) but unfortunately it tells me : BCE0022: Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Transform'.

avatar image GarretLawrence seb-lopez · Jan 16, 2017 at 11:00 PM 0
Share

Oh sorry it should be : currentTarget = player[0].transform;

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

99 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 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

[I REALLY NEED HELP FAST]Help with enemy Shooting 1 Answer

Boxing character and AI enemys 2 Answers

Problem with the movement of AI 1 Answer

Check lenght from player? 2 Answers

How do you make a enemy AI hear approaching footsteps? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges