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 f9073 · Jun 15, 2016 at 06:19 AM · errorenemytagstrackingmultiple objects

How to keep track of multiple NPC's / Enemies

Hi there,

I am making a defence game, where the player places soldiers which can be moved along the walls/buildings etc

Each soldier has a light of sight in the form of an arc, if an enemy enters the line of sight, defined by a fixed amount of degrees and range, they will engage that enemy until they have left the line of sight or are dead.

I have that working, however, I am only tracking one enemy at a time. The game is going to have large numbers of each type of enemy on screen.

Which is the best way to track these, and which is the best way to pass the information to the soldier that they can shoot that particular enemy i.e. they won't be able to shoot an enemy that flies, unless they have a particular weapon.

Currently I have a variable for each enemy, but the problem arises with targeting more than one. I have included my script below.

 using UnityEngine;
 using System.Collections;
 
 public class SODLineOfSightCol : MonoBehaviour {
 
     public float fieldOfViewAngle = 110.0f; //what angle of the sphere collider will be used
     public bool npcInSight; //is the npc in sight -- may need to be changed to look for a Rigidbody with a certain tag or layer
 
     private SphereCollider col;
     GameObject[] npc;
     float soldierRotationTime = 3.5f;
 
     LineRenderer line;
 
     void Awake () {
 
         closest = null;
         col = GetComponent<SphereCollider>(); //find the sphere collider
         npc = GameObject.FindGameObjectsWithTag("npc");
         Debug.Log(npc1);
     }
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
             //get soldiers original rotation
             //this will be used to reset after it has killed an enemy
 
             //check if the NPC is still alive, if so, turn to face and shoot etc...
 
             //find the closest enemy to the soldier
             //FindClosestEnemy();
     }
 
     void OnTriggerStay(Collider other) { //called once per game for every Collider that is touching the trigger
 
         //get 2x Vector 3's, one fo the angle of the Vector to the Player
             //another for the Soldiers Forward Vector
         
         Vector3 direction = other.transform.position - transform.position;
         float angle = Vector3.Angle(direction, transform.forward);
 
         if(angle < fieldOfViewAngle * 0.5f){ //check if the angle is less than half of the fieldOfViewAngle
 
             //create a ray to see if an object is blocking the line of sight
 
             RaycastHit hit;
 
             //if(Physics.Raycast(transform.position + transform.up, direction.normalized, out hit, col.radius)){
             if(Physics.Raycast(transform.position, direction.normalized, out hit, col.radius)){
                 //transform.up is used to move the ray up 1 unit
                 //direction.normalized is used to keep a vector in the same direction, but its length at 1.0f
                 //col.radius is used to keep the ray the length of the sphere colliders radius, as there is no need for it to be longer
 
                 if(hit.collider.gameObject == npc){
 
                     npcInSight = true;
                     //inform solider to face the npc
                     //call funciton to start shooting
 
                     Transform targetNPC = npc.transform;
                     Quaternion targetRotation = Quaternion.LookRotation(targetNPC.position - transform.position);
                     //Quaternion.LookRotation(targetNPC.position - transform.position); finds the direction from the transform.position to target.position
                     float strength = Mathf.Min(soldierRotationTime*Time.deltaTime,1);
                     transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, strength);
 
 
                 }
 
             }
         }
     }
 
     void OnTriggerExit(Collider other){
 
         if(other.gameObject == npc)
             npcInSight = false;
         
     }
 }

I have tried to use, FindObjectsWithTags in the hope it will be able to use all of the enemies I have tagged with 'npc' but it just seems to chuck up this error "`==' cannot be applied to operands of type UnityEngine.GameObject' and UnityEngine.GameObject[]'"

Apologies for all the comments etc... Any help greatly appreciated!

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I get the name of the object I am attacking and how can I use it to select the game object name? 0 Answers

Creating multiple text components on a canvas 1 Answer

Enemy AI Error 3 Answers

getting error "Operator '<' cannot be used with a left hand side of type 'Object' and a right hand side of type 'float'." 0 Answers

My targeting system isn't targeting the character 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