- Home /
[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;
}
Answer by Perfecter · Jul 09, 2018 at 02:34 PM
Hi, @bambosteak.
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).
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;
}
}
Answer by madks13 · Jul 09, 2018 at 02:02 PM
You should have a Die() method on your destructible objects. Avoids having to copy-pasta destruction code.
Maybe destroy the current enemy AFTER checking distances?
What is prevDist? How do you initialize/reinitialize it?
Why instantiate new bullets? Can't you reuse the same game object?
Use bullet.LookAt(newEnemy.transform) to set the direction of the bullet
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);
}
}
}
Hey, thanks for answering.
I have a Die() method attached to an enemyhealth script on my enemies.
I tried doing it this way around, but it still produces the same results.
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.
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.
LookAt seems to produce the same result aswell.
I'll keep this in $$anonymous$$d, ty.
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.
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.
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;
}
Your answer
Follow this Question
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