[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*.layer == 9)*

{
distance = Vector3.Distance(other.transform.position, enemies*.transform.position);*
if (distance < prevDist)
{
prevDist = distance;
newEnemy = enemies*;*
}
}
}
}
var bullet = (GameObject)Instantiate(bulletPrefab, other.transform.position, other.transform.rotation);
Physics.IgnoreCollision(bullet.GetComponent(), other);
Vector3 dir = newEnemy.transform.position - other.transform.position;
bullet.GetComponent().AddForce(dir * 6f);
}
other.gameObject.layer = 9;
}

  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);
            }
        }
    }

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*;*

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(), other);
Vector3 dir = newEnemy.transform.position - other.transform.position;
bullet.GetComponent().AddForce(dir * 6f);
}

}
other.gameObject.layer = 9;
}
}

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*;*

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(), other.GetComponent());
bullet.GetComponent().AddForce(dir * 3000f);
Debug.DrawLine(other.transform.position, newEnemy.transform.position, Color.red);
Destroy(bullet, 2f);
}
other.gameObject.layer = 9;
}