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 seeker727 · Dec 04, 2018 at 05:58 PM · bugsprojectiles

Projectile selectively passing through enemies

Greetings,

I am having problems fixing a bug with my isometric 2d action game.

The fireball projectile I have created will hit and kill an enemy, but when there are multiple enemies present, it seems to select only one enemy that it will hit and passes through all other enemies.

When the "selected" enemy dies, it will select a different enemy and pass through all others.

How do I fix this?

The script I am using for the fireball can be found below:

public class FireballController : MonoBehaviour {

 //reference variables for use with damage, collision, and position
 GameObject enemy;
 EnemyHealth enemyHealth;
 Animator anim;
 private Rigidbody2D rb2d;

 public int attackDamage = 100;  //Damage per fireball hit
 public float speed = 30; //initial force added to fireball projectile


  
 void Start ()
 {
     //setup reference variables for damage, collision, and position
    rb2d = GetComponent<Rigidbody2D>();
    rb2d.AddForce(transform.up * speed); //adds initial force to fireball when spawned

    Destroy(gameObject, 5);
    
    anim = GetComponent<Animator>();
 }
 


 void OnTriggerEnter2D(Collider2D other)
 {
     // If the entering collider is the enemy...
     if (other.gameObject == GameObject.FindGameObjectWithTag("Enemy"))
     {
         enemy = GameObject.FindGameObjectWithTag("Enemy");
         enemyHealth = enemy.GetComponent<EnemyHealth>();
         // ... damage the enemy.
         enemyHealth.TakeDamage(attackDamage);

         rb2d.velocity = new Vector2(0, 0);

         anim.SetBool("explode", true);

         Destroy(gameObject);
     }
 }
 

}

Please note that this is my first game in Unity and my first time posting on the forums. I am aware that I have no idea what I'm doing, so helpful comments only please :)

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
1

Answer by Vega4Life · Dec 04, 2018 at 06:03 PM

It seems to be this:


 if (other.gameObject == GameObject.FindGameObjectWithTag("Enemy"))



This will find the same enemy every time. Instead, maybe just do this:


        if (other.gameObject.tag == "Enemy"))


Then you should get what you want.


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
0

Answer by seeker727 · Dec 05, 2018 at 02:23 AM

Thank you for your assistance!

Initially, this solved the problem of the projectile passing through enemies, but it then caused the projectile to sometimes affect a different enemy than the one it collided with.


I then also changed

 enemy = GameObject.FindGameObjectWithTag("Enemy"); 

to

 enemy = other.gameObject;



And the bug is completely fixed!

Thank you for your assistance and for teaching me something today :)

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 Vega4Life · Dec 05, 2018 at 02:44 AM 0
Share

Awesome. Good luck!

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

95 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

Related Questions

Asset store Crash how do I fix ? 1 Answer

How do i fix this? 0 Answers

[Solved] Bug with disabling Polygon Collider 2D 0 Answers

Bugs in a pick up script 1 Answer

Why does empty if statement fix null reference 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