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 corriedotdev · Feb 01, 2016 at 11:03 PM · c#enemies

Targetting enemy, repeating code

Hi all,

So my issue is i have produced messy code. I have a gun and a bullet. In a fixedupdate in the gun, it searches through all the spawned enemies and stored the closest in a variable called myTarget. Then when the player presses a ui element, a bullet is instantiated. Ideally in the start() of the instantiated bullet object i have something like enemy = player.GetComponent().GetTarget(); Which should return the myTarget. However no matter what i tried various things would happen, a bullet would spawn with no target even though one is passed. So now ive created that same loop through all the enemies to search for the target again, it might not even be the same as the myTarget in the gun now!

     public GameObject NearestEnemy() {
 
         enemies = GameObject.FindGameObjectsWithTag("Enemy");
         myTarget = null;
         float distance = Mathf.Infinity;
         Vector3 position = transform.position;
         foreach (GameObject enemy in enemies){
             Vector3 diff = enemy.transform.position - position;
             float curDistance = diff.sqrMagnitude;
             if (curDistance < distance){
                     myTarget = enemy;
                     distance = curDistance;
             }
             if (myTarget == enemy){
                 //Debug.Log("My closest selected enemy so far is: " + myTarget.transform.name);
             }
         }
             return myTarget;
         }
     
 
     public void LazerAttack(){
         if (myTarget == null){
 
         } else if (Vector3.Distance(myTarget.transform.position, transform.position) < range && ammo > 0 && !bulletSpawned && myTarget != null){// if myTarget is in range then fire AND bullet spawned false
             //Debug.Log(" target is " + myTarget);
             bulletSpawned = true;
             //Debug.Log("set bullet spawned to " + bulletSpawned);
             Instantiate(bulletPrefab, transform.position, transform.rotation); // spawn bullet at the players current pos
       
             ammo--;
         }
         //Debug.Log ("ATTACK BUTTON PRESSED");
     }

So i have it working with essentially the same script which loops through all the enemies and returns the closest. Which is messy and not good. Anyone know how i can pass the myTarget to the instantiated bullet. In the fixedupdate of the buller i have a slerp which follows the nearest enemy.

Ive tried creating a setter in the bullet but no luck, it just fires into space. Ive also tried a getter in the bullet to the gun to get the target with no luck either! Thanks for the help

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 SterlingSoftworks · Feb 02, 2016 at 12:49 AM 0
Share

One thing you could try is setting up a clone of your spawned GameObject in script with a line of code like

 GameObject clone = (GameObject)Instantiate (GameObjectYouWantToSpawn, positionOfSpawn, Quaternion.identity);            

Then you can access a public function in your "cloned" object and set stuff there.

 clone.GetComponent<ScriptYouAreAccessingHere>().setTargetFunction(target);

Not sure if this will work but it's worth a shot :D

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by JoshuaMcKenzie · Feb 02, 2016 at 02:08 AM

after spawning the bullet get the bullet's script and simply pass in the value.

     public float range;
     public int ammo;
     public GameObject bulletPrefab;
     
     private GameObject myTarget;
     private float targetRange;
     
     public void LazerAttack()
     {
         GetNearestEnemy();
         
         if(myTarget  &&  ammo>0  &&  targetRange  <  range)
         {
             GameObject bullet = GameObject.Instantiate<GameObject>(bulletPrefab);
             bullet.transform.position = transform.position;
             bullet.transform.rotation = Quaternion.identity;

             //you need to get the bullet's monobehaviour to set its target after instantiating
             BulletScript script = bullet.GetComponent<BulletScript>();
             script.target = myTarget;
             
             --ammo;
         }
     }
     
     
     void GetNearestEnemy()
     {
         myTarget = null;
         targetRange = 0;
         
         //We don't need to use FindGameObjectWithTag!
         //enemies = GameObject.FindGameObjectsWithTag("Enemy");
         
         //This is much faster!
         foreach ( GameObject enemy in EnemyScript.Enemies)
         {
             float currDistance  = (enemy.transform.position -  transform.position).magnitude;
             
             if(!myTarget || currDistance<targetRange )
             {
                 myTarget = enemy;
                 targetRange  = currDistance;
             }
         }
     }

With BulletScript being the actual monobehavior you have on your bullet (do note that the bullet script won't have access to the myTarget variable in the Awake(), since that runs immeadiately when you instantiate the bullet prefab).

Also FindGameObjectsWithTags is a pretty slow function (and performance might be a factor depending on how many times you call LazorAttack() each second). instead if you just slightly prep your enemy's monobehavior script you can possibly get a noticeable increase in framerate.

 using System.Collections.Generic;
 
 public class EnemyScript : MonoBehaviour
 {
     private static List<GameObject> enemies = new List<GameObject>();
     
     public static List<GameObject> Enemies
     {
         get
         {
             return new List<GameObject>(enemies);
         }
     }
     
     void OnEnable()
     {
         enemies.Add(gameObject);
     }
     
     void OnDisable()
     {
         enemies.Remove(gameObject);
     }
 }


Comment
Add comment · Show 2 · 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 corriedotdev · Feb 02, 2016 at 06:10 PM 0
Share

Hi! Thanks for the answer, ive been reading it and implementing what you suggested. However im struggling to use the enemy script, which is in turn failing to instantiate a bullet. As the user is rarely pressing lazer attack and the array of enemies is 8 tops id like to try first getting the findwithtag working, then perhaps explore more options if i need more speed. So in the Bullet script i have a variable called target. This should then work correct? So in the start of the bullet, im calling the nearest enemy script, which value it returns is my target. by

  target = player.GetComponent<Gun>().NearestEnemy();

But the game targets the first enemy it appears regardless if the target changes?

     public GameObject NearestEnemy(){
         enemies = GameObject.FindGameObjectsWithTag("Enemy");
         GameObject myTarget = null;
         float distance = $$anonymous$$athf.Infinity;
         Vector3 position = transform.position;
         foreach (GameObject enemy in enemies){
             Vector3 diff = enemy.transform.position - position;
             float curDistance = diff.sqr$$anonymous$$agnitude;
             if (curDistance < distance){
                 myTarget = enemy;
                 distance = curDistance;
             }
         }
         return myTarget;
     }


avatar image JoshuaMcKenzie corriedotdev · Feb 02, 2016 at 11:24 PM 0
Share

If the code is still not working then I assume its due to how the code is running in the other scripts. Possibilities can be what ever is calling LazorAttack (possible but unlikely the culprit). what ever code you have in your bullet script, or what you have in your enemy script. since none of these have been provided and can only go on the code presented. if you can post the bullet script and possibly the enemy script then the issue might be pinpointed (since your using Find with tag you should make sure the enemy has that exact tag (letter casing matters) and that the scripts are enabled. make sure the bullet's script is active when spawned.

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

68 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

Related Questions

How Do I Instantiate Multiple Coins Once The Enemy Dies? 1 Answer

Multiple different bullet damages for multiple enemies 1 Answer

Multiple enemies and no animations 0 Answers

How i can damage more than one enemy? 1 Answer

Making a RPG video Tutorial question 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