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 LucasFullStop · Feb 13, 2019 at 04:30 AM · 2daishootingbullet

Only one bullet AI works?

I have an AI that moves towards the player and shoots, but for some reason when there are multiple enemies, the enemy itself does keep moving towards the player but the bullet only shoots out of the last enemy spawned. Am I doing something wrong? Here's the code:

 public Rigidbody2D rb2d;
 public Collider2D pc2d;

 public float moveSpeed;
 public float damage = 10f;

 private void OnEnable()
 {
     StartCoroutine(Shoot());
     Physics2D.IgnoreCollision(pc2d, FindObjectOfType<Enemy1Controller>().enemy1Collider);
 }

 IEnumerator Shoot()
 {
     Vector3 target = FindObjectOfType<PlayerController>().player.transform.position - transform.position;

     rb2d.AddForce(target.normalized * moveSpeed * Time.deltaTime, ForceMode2D.Impulse);
     rb2d.AddTorque(10000f, ForceMode2D.Impulse);


     yield return new WaitForSeconds(2f);

     Destroy(gameObject);
 }

 private void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.collider.CompareTag("Player"))
     {
         FindObjectOfType<PlayerController>().TakeDamage(damage);
     }

     Destroy(gameObject);
 }
Comment
Add comment · Show 2
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 RobAnthem · Feb 13, 2019 at 04:55 AM 0
Share

I believe what is happening here is that you're IgnoreCollision function is only finding the last spawned enemy, and not ignoring collisions for the enemies firing it. I usually have an initialization function in my projectiles and pass the attacker to the projectile for relevant functions like this.

Also.... you're scaling your bullets speed by Time.deltaTime, in a coroutine, which makes no sense because the coroutine doesn't run on the update thread, so it's actually only firing that function once and scaling it by delta time once. A bullet should have its own script to handle velocity, or you should create a bullet manager that manages the velocity of all instantiated bullets as well as object pools them to save on resources.

avatar image LucasFullStop RobAnthem · Feb 13, 2019 at 05:28 AM 0
Share

Thanks Rob, yeah that's correct, I changed the collider to trigger and the bullet does fire. I tried removing Time.delaTime but the bullets do move a lot faster. Does that mean I just need to decrease moveSpeed and it will be fine or do I need to do something else? I'm fairly new to Unity, so I'm not sure what you mean by "initialization function" and velocity scripts and things like that.

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

253 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

Related Questions

Shooting style like enter the dungeon 0 Answers

Transform.translate bullets problem 0 Answers

AI reflect shooting 0 Answers

Button Shooting in the direction of Player 0 Answers

How to make 3 bullets fire at different angles 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