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 bambosteak · Jul 09, 2018 at 11:16 AM · enemydirectionprojectileeffectchain

[Solved]Chaining projectiles

Hi, I'm attempting to create a chaining effect for my projectile. The idea is that I want to shoot a projectile and when said projectile hits an enemy, I want it to find the closest enemy to the enemy I just hit and shoot a projectile in that direction.

The way i've done it so far is something like this: When an enemy has been hit I make an array of all the enemies in the level. I loop through each one, except for the one I hit, and calculate the distance between them. The enemy with the shortest distance is assigned to a new enemy variable. I then instantiate my projectile, calculate a new direction vector between the new enemy and the enemy I previously hit, and add a force to that direction on the new projectiles rigidbody.

The effect i'm getting is that the new projectile shoots off in the same direction as the original projectile the first time the enemy is hit. The second time the enemy's health drops to 0 and it dies, resulting in the new projectile simply standing still.

Is there anything i've missed in my theory here?

Here's some of my code of the effect: Notes: layer is used to make sure not to calculate distance with the enemy that was originally hit. The script is on the projectile

 private GameObject[] enemies;
 private GameObject newEnemy;

 private void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Enemy")
     {
         Destroy(gameObject);
         other.gameObject.layer = 10;

                 enemies = GameObject.FindGameObjectsWithTag("Enemy");
     if (enemies.Length > 1)
     {
         for (int i = 0; i < enemies.Length; i++)
         {
             if (enemies[i].layer == 9)
             {
                 distance = Vector3.Distance(other.transform.position, enemies[i].transform.position);
                 if (distance < prevDist)
                 {
                     prevDist = distance;
                     newEnemy = enemies[i];
                 }
             }
         }
     }
     var bullet = (GameObject)Instantiate(bulletPrefab, other.transform.position, other.transform.rotation);
     Physics.IgnoreCollision(bullet.GetComponent<Collider>(), other);
     Vector3 dir = newEnemy.transform.position - other.transform.position;
     bullet.GetComponent<Rigidbody>().AddForce(dir * 6f);
     }
     other.gameObject.layer = 9;
 }





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

3 Replies

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

Answer by Perfecter · Jul 09, 2018 at 02:34 PM

Hi, @bambosteak.

  1. You got same direction because you doesnt miss current object. You checking whenether enemies count is bigger than 1 but then you check all enemies you have in array (including "other" one).

  2. Whenever you dont find any enemy to hit you still instantiate bullet. Try check it for null before instantiate.

Here is a code that will provide behaviour you want (or smth very close to it):

 using UnityEngine;
 using System.Collections;
 
 public class NewMonoBehaviour : MonoBehaviour
 {
 
     private GameObject[] enemies;
     private GameObject newEnemy;
     private void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.tag == "Enemy")
         {
             Destroy(gameObject);
             other.gameObject.layer = 10;
             enemies = GameObject.FindGameObjectsWithTag("Enemy");
 
             for (int i = 0; i < enemies.Length; i++)
             {
                 var enemy = enemies[i];
 
                 if (enemy.transform != other.transform && enemy.layer == 9)
                 {
                     distance = Vector3.Distance(other.transform.position, enemy.transform.position);
                     if (distance < prevDist)
                     {
                         prevDist = distance;
                         newEnemy = enemy;
                     }
                 }
             }
 
             if (newEnemy != null)
             {
                 var bullet = (GameObject)Instantiate(bulletPrefab, other.transform.position, other.transform.rotation);
                 Physics.IgnoreCollision(bullet.GetComponent<Collider>(), other);
                 Vector3 dir = newEnemy.transform.position - other.transform.position;
                 bullet.GetComponent<Rigidbody>().AddForce(dir * 6f);
             }
             
         }
         other.gameObject.layer = 9;
     }
 }
 








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 madks13 · Jul 09, 2018 at 02:02 PM

  1. You should have a Die() method on your destructible objects. Avoids having to copy-pasta destruction code.

  2. Maybe destroy the current enemy AFTER checking distances?

  3. What is prevDist? How do you initialize/reinitialize it?

  4. Why instantiate new bullets? Can't you reuse the same game object?

  5. Use bullet.LookAt(newEnemy.transform) to set the direction of the bullet

  6. Avoid using a Destroyed object.

Edit :

@bambosteak I added a corrected and simplified version of your code :

     using UnityEngine;
     using System.Linq;
 
     public class Bullet : MonoBehaviour
     {
         [SerializeField]
         private GameObject bulletPrefab;
         private GameObject[] enemies;
         private GameObject newEnemy;
 
         public float distance;
         public float prevDist = float.MaxValue;
 
         private void OnTriggerEnter(Collider other)
         {
             if (other.gameObject.tag == "Enemy")
             {
                 //Get all references
                 //Except the collided object
                 enemies = GameObject.FindGameObjectsWithTag("Enemy").Where(e => !e.Equals(other)).ToArray();
                 newEnemy = null;
                 prevDist = float.MaxValue;
 
                 //Iterate over enemy list
                 foreach (var enemy in enemies)
                 {
                     //No need to check for this
                     //Since the collided object is not in the list
                     //Unless it also checvks for something else
                     //if (enemy.layer == 9)
                     //{
                         distance = Vector3.Distance(other.transform.position, enemy.transform.position);
                         if (distance < prevDist)
                         {
                             prevDist = distance;
                             newEnemy = enemy;
                         }
                     //}
                 }
                 
                 //Only if we get a new enemy target
                 //Do we create a new bullet
                 if (newEnemy != null)
                 {
                     var bullet = (GameObject)Instantiate(bulletPrefab, other.transform, true);
                     bullet.transform.LookAt(newEnemy.transform);
                     bullet.GetComponent<Rigidbody>().AddRelativeForce(Vector3.forward * 6f, ForceMode.Force);
                 }
 
                 //Destroy current enemy
                 Destroy(gameObject);
             }
         }
     }
 
 

Comment
Add comment · Show 7 · 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 bambosteak · Jul 09, 2018 at 02:17 PM 0
Share

Hey, thanks for answering.

  1. I have a Die() method attached to an enemyhealth script on my enemies.

  2. I tried doing it this way around, but it still produces the same results.

  3. prevDist was set to an extremely high number, but was never reinitialized. However, i tried reinitializing it now and it does not affect the result.

  4. Instantiating new bullets was the way I thought about it when I came up with the idea. I'll try to reuse the same object.

  5. LookAt seems to produce the same result aswell.

  6. I'll keep this in $$anonymous$$d, ty.

avatar image madks13 bambosteak · Jul 09, 2018 at 02:21 PM 0
Share

Can you try to do the test with 3-4 enemies and check each line in your method? there is a variable that misbehaves and it's hard to tell from your code. From your description, it seems like the direction is the problem, but if LookAt gives the same result, i'd guess you're targeting the wrong enemy/transform for some reason.

avatar image bambosteak madks13 · Jul 09, 2018 at 02:37 PM 0
Share

I did some testing and it seems it does shoot off in the right direction now, but the force I was adding to the object was so small that it appeared to be standing still. Also now I don't get the effect that makes it look like I pierce the enemy, because I think the new bullet is hitting the enemy from inside until it dies.

I tried lowering the projectiles damage, but the projectile seems to be stuck inside the enemy until its dealt enough damage to kill it, and then it will shoot out. Also meanwhile it is stuck in the enemy the enemy is translated to the left each time it takes a hit.

Show more comments
avatar image
0

Answer by bambosteak · Jul 09, 2018 at 05:07 PM

Okay. It is almost completely fixed now.

I added the dir variable to the position parameter of the instantiation of the bullet. Now it is no longer hitting itself with new bullets.

Now they bounce between the closest enemies until one dies, and then the bullet fly towards the enemy that died, as if not registering it being dead yet.

I think this is fixed by modifying my other scripts tho.

Here's an updated version of the script.

Thanks everybody for helping me out!

 public GameObject bulletPrefab;

 private GameObject[] enemies;
 private GameObject newEnemy;
 private GameObject currentEnemy;
 private float prevDist;
 private float distance;

 private void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Enemy")
     {
         Destroy(gameObject);
         prevDist = float.MaxValue;
         newEnemy = null;
         other.gameObject.layer = 10;
         Chain(other);

     }
     other.gameObject.layer = 9;
 }


 void Chain(Collider other)
 {
     enemies = GameObject.FindGameObjectsWithTag("Enemy");
     for (int i = 0; i < enemies.Length; i++)
     {
         currentEnemy = enemies[i];

         if (currentEnemy.transform != other.transform && currentEnemy.layer == 9)
         {
             distance = Vector3.Distance(other.transform.position, currentEnemy.transform.position);
             if (distance < prevDist)
             {
                 prevDist = distance;
                 newEnemy = currentEnemy;
             }
         }
     }

     if (newEnemy != null)
     {
         Vector3 dir = Vector3.Normalize(newEnemy.transform.position - other.transform.position);
         var bullet = (GameObject)Instantiate(bulletPrefab, other.transform.position + dir, other.transform.rotation);
         Physics.IgnoreCollision(bullet.GetComponent<Collider>(), other.GetComponent<Collider>());
         bullet.GetComponent<Rigidbody>().AddForce(dir * 3000f);
         Debug.DrawLine(other.transform.position, newEnemy.transform.position, Color.red);
         Destroy(bullet, 2f);
     }
     other.gameObject.layer = 9;
 }
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

91 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

Related Questions

How can i make a projectile attack travel through multiple enemies dealing damage to all of them? 1 Answer

Player's projectile communicating with Enemy 3 Answers

Instantiating in a position relative to an object. 1 Answer

How to get bullet to face the direction it's going? 3 Answers

How to detect wether an object is headed towards the top or bottom of an object? 2 Answers


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