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
0
Question by BDR530 · May 11, 2020 at 07:52 PM · 2dinstantiatearrayvector3gameobjects

Multiple Fire Points

Hi, i'm trying to create a 2D game like missile command, i use this script to spawn enemy bullets:

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class EnemyShots : MonoBehaviour
  {
      public GameObject enemyBullet;
      public GameObject enemyFirePoint;
      private GameObject[] cities;
  
      public float waitingTime;
      public float bulletSpeed;
  
      float timer;
  
      void Start()
      {
          cities = GameObject.FindGameObjectsWithTag("City");
      }
      void Update()
      {
          timer += Time.deltaTime;
  
          if (timer > waitingTime)
          {
              Vector3 difference = enemyFirePoint.transform.position - cities[Random.Range(0, cities.Length)].transform.position;
              float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
  
              float distance = difference.magnitude;
              Vector2 direction = difference / distance;
  
              direction.Normalize();
              Shoot(direction, rotationZ);
  
              timer = 0;
          }
      }
  
      void Shoot(Vector2 direction, float rotationZ)
      {
              GameObject e = Instantiate(enemyBullet) as GameObject;
              e.transform.position = enemyFirePoint.transform.position;
              e.transform.rotation = Quaternion.Euler(0, 0, rotationZ);
              e.GetComponent<Rigidbody2D>().velocity = (direction * -1) * bulletSpeed;
      }
  
  }

How can i add multiple fire points (array) to the script? Thanks!

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

2 Replies

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

Answer by darksider2000 · May 11, 2020 at 08:36 PM

I've never played the game, so I'm not exactly sure by your description what you're looking for.

If your multiple fire points are supposed to be predetermined in the scene before the game starts then you can use a Transform array.

If your points are supposed to be random you can use insideUnitCircle or Random.Range


Here's a method utilizing a Transform array - You set your transforms up in the scene, drag and drop into the array, and those are your firing points.

 public Transform[] firePoints = new Transform[3]; // Choose any size you want
 
 void Shoot(Vector2 direction, float rotationZ)
       {
              Quaternion rotation = Quaternion.Euler(0f, 0f, rotationZ);
              foreach (Transform firePoint in firePoints) {
                    // Instantiate returns GameObject by default, no need to cast
                    GameObject e = Instantiate(enemyBullet, firePoint, rotation); 
                    e.GetComponent<Rigidbody2D>().velocity = (direction * -1) * bulletSpeed;
              }           
       }

Your firePoints array will also keep rotation and scale, in case you want to spawn bullets with different rotation/scale values.

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
1

Answer by ADiSiN · May 11, 2020 at 08:48 PM

Hi!

I hope I understood you correctly and the reason for multiple fire points it's to calculate and call Shoot() function for each one of them. You can do it this way and each time when timer > waitingTime it will iterate through all array and execute your code.

 public GameObject[] enemyFirePoint;
 
 void Update()
 {
     timer += Time.deltaTime;
 
     if (timer > waitingTime)
     {
         for(int i = 0; i < enemyFirePoint.Length; i++)
         {
             Vector3 difference = enemyFirePoint.transform.position - cities[Random.Range(0, cities.Length)].transform.position;
             float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
 
             float distance = difference.magnitude;
             Vector2 direction = difference / distance;
 
             direction.Normalize();
             Shoot(direction, rotationZ);
 
             timer = 0;
         }
     }
 }

However, keep in mind that it will execute for all point "at once" - in single frame. If you want to make some delay, for example wait couple of second before execute calculations for the second points then you can use IEnumerators like in code below:

 public GameObject[] enemyFirePoint;
 
 void Update()
 {
     timer += Time.deltaTime;
 
     if (timer > waitingTime)
     {
         timer = 0;
 
         StartCoroutine(ExecuteFirePoints());
     }
 }
 
 IEnumerator ExecuteFirePoints()
 {
     for(int i = 0; i < enemyFirePoint.Length; i++)
     {
         Vector3 difference = enemyFirePoint.transform.position - cities[Random.Range(0, cities.Length)].transform.position;
         float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
 
         float distance = difference.magnitude;
         Vector2 direction = difference / distance;
 
         direction.Normalize();
         Shoot(direction, rotationZ);
 
         yield return new WaitForSeconds(1f);
     }
 }

Remember that for IEnumerators you must have using System.Collections library. Also, since you are using enemyFirePoint.transform.position in your Shoot() function you can either re-design it or pass additional variable to access certain object in array, or you can apply darksider2000 great method, that's up to you.


Let me know if it helps you or if I misunderstood your question.

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

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

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

instantiate problem help? 1 Answer

Issue with instantiating multiple clone GameObjects 1 Answer

Finding the Sum of Values of Multiple GameObjects in an Array + Variable Sized arrays 0 Answers

Position + Vector3 doesn't return correct values 1 Answer

2D Array of GameObjects... 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