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 /
avatar image
1
Question by jordano00 · Feb 28, 2017 at 07:41 AM · c#unity 5instantiateenemy spawnindependent

how to make enemy clone work independently?

so I am making a shooting game where the game spawns 5 enemies from a prefab. the problem is that when the enemy clone gets instantiated it works properly, but when the enemy clone detects the player, all of the enemy clones start shooting at the player.

I want the enemy clone that detected the player to be the only one that will shoot at the player, and not all of the enemy clones.

here is the code for my enemy object:

 using UnityEngine;
 using System.Collections;
 
 public class enemyMovement : MonoBehaviour
 {
     Rigidbody2D rb2d;
     public Transform target;
     private float nextFire;
     public float turnTime;
     public float speed;
     public float trackSpeed;
     int turn;
     public GameObject particles;
     int enemyHp = 100;
 
     private float explodeTime;
     // Use this for initialization
     void Start()
     {
         rb2d = GetComponent<Rigidbody2D>();
         particles.gameObject.SetActive(false);
         target = GameObject.Find("fighter").transform;
     }
     float h, v, zed;
     
    public static bool check = true;
     // Update is called once per frame
     void Update()
     {
        
         float horizontal = Input.GetAxis("Horizontal");
         float vertical = Input.GetAxis("Vertical");
         if (check == true)
         {
             if (Time.time > nextFire)
             {
                 nextFire = Time.time + turnTime;
                 turn = Random.Range(0, 5);
                 switch (turn)
                 {
                     case 1:
 
                         h = horizontal - 1;
                         v = vertical;
                         rb2d.transform.eulerAngles = new Vector3(0, 0, 90);
                      //   Debug.Log("left");
                         break;
                     case 2:
 
                         h = horizontal + 1;
                         v = vertical;
                         rb2d.transform.eulerAngles = new Vector3(0, 0, 270);
                      //   Debug.Log("right");
                         break;
                     case 3:
 
                         h = horizontal;
                         v = vertical + 1;
                         rb2d.transform.eulerAngles = new Vector3(0, 0, 0);
                      //   Debug.Log("up");
                         break;
                     case 4:
                         h = horizontal;
                         v = vertical - 1;
                         rb2d.transform.eulerAngles = new Vector3(0, 0, 180);
                        // Debug.Log("down");
                         break;
 
 
                 }
 
             }
             Vector2 temp = new Vector2(h, v);
             rb2d.AddForce(temp * Time.deltaTime * speed);
         }
        else if (check == false)
         {
             Vector3 vectorToTarget = target.position - transform.position;
             float angle = (Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg) - 90;
             Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
             transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * speed);
 
             if ((target.position.x > rb2d.position.x + 7 && target.position.x < rb2d.position.x + 15)
                 || (target.position.x < rb2d.position.x - 7 && target.position.x > rb2d.position.x - 15)
                 || (target.position.y > rb2d.position.y + 5 && target.position.y < rb2d.position.y + 15)
                 || (target.position.y < rb2d.position.y - 5 && target.position.y > rb2d.position.y - 15))
             {
                 transform.position += transform.up * Time.deltaTime * trackSpeed;
             }//if the enemy is within the range of the player, the enemy will follow the player.
             else if(target.position.x > rb2d.position.x + 15 || target.position.x < rb2d.position.x - 15 || target.position.y > rb2d.position.y + 15 || target.position.y < rb2d.position.y - 15)
             {
                 check = true;
              
             }//if the enemy is outside the range of the player, the enemy will resume its random movement.
           
         }
 
         if(enemyHp == 0)
         {
             Destroy(gameObject);
             enemySpawn.maxEnemy--;
             particles.gameObject.SetActive(true);
             if (Time.time > explodeTime)
             {
                 explodeTime = Time.time + .5f;
                 particles.gameObject.SetActive(false);
             }
             }
       //  Debug.Log(check);
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if(other.gameObject.CompareTag("player fighter"))
         {
             check = false;
          
         }// if the player touches the onTrigger for the enemy, the enemy will follow the player.
     }
 
     void OnCollisionEnter2D(Collision2D other)
     {
         if (other.gameObject.CompareTag("player bullet"))
         {
             Destroy(other.gameObject);
             enemyHp -= bulletMove.bulletDamage;
 
         }
     }
 }
 
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by eldruz · Feb 28, 2017 at 02:18 PM

It seems the boolean you use for player detection, check, is a static variable. As such, it is shared by all instances of your class, meaning that once an enemy sees the player, they all do.

You should just use a non static variable for player detection.

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 Xitech_ · Feb 28, 2017 at 02:50 PM 1
Share

And maybe look into when to use static.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i Instantiate gameObjects in between multiple points? 2 Answers

How do i Instantiate Objects to a Empty GameObject as a Child?? 3 Answers

Instantiating at different rates 1 Answer

Instantiated object not showing in scene or hierarchy 2 Answers

How do i Instantiate sub-Points with in Multiple Points?? 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