Hello I'm new to coding on General but I've been going at it for about a year on and off and I'm asking for help now that I feel like I'm understanding things much better.
Goal = Make a bullet that can bounce between all nearby targets to the first target hit i.e Sivir's W from League of Legends, Chain Lightning from World of Warcraft.
Problem = Each bullet isn't treated like an individual, but they all despawn after the loop completes. I want each bullet to jump "bounce" amount of times.
End game is the make a system that lets the player gain incremental buffs while surviving waves of enemies i.e vampire Survivor.
Thank you for your time and your contribution to keeping my game dev dream truckin'.
"ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <9aad1b3a47484d63ba2b3985692d80e9>:0)
Bullet.Bounce () (at Assets/Scripts/Bullet.cs:92)
Bullet.HitTarget () (at Assets/Scripts/Bullet.cs:49)
Bullet.Update () (at Assets/Scripts/Bullet.cs:32"
**CODE**
public class Bullet : MonoBehaviour
{
public Transform target;
public float explosionRadius = 0f;
public float bounceRadius = 20f;
public int bounce = 3;
public float speed = 10f;
public void Seek(Transform _target)
{
target = _target;
}
void Update()
{
if(target == null)
{
Destroy(gameObject);
return;
}
Vector3 dir = target.position - transform.position;
float distanceThisFrame = speed * Time.deltaTime;
if(dir.magnitude <= distanceThisFrame)
{
HitTarget();
return;
}
transform.Translate(dir.normalized * distanceThisFrame, Space.World);
}
void HitTarget()
{
/*if (explosionRadius > 0f)
{
Explode();
}*/
if (bounce > 0)
{
Bounce();
}
else
{
Damage(target);
}
}
void Explode()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, explosionRadius);
foreach(Collider2D collider in colliders)
{
if(collider.tag == "Enemy")
{
Damage(collider.transform);
Destroy(gameObject);
}
}
}
private void Bounce()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, bounceRadius);
List validTargets = new List();
foreach (Collider2D collider in colliders)
{
if (collider.CompareTag("Enemy"))
{
validTargets.Add(collider.transform);
}
}
for (int i = 0; i < validTargets.Count; i++)
{
if (i==bounce)
{
Die();
}
else
{
Seek(validTargets[i]);
Damage(target.transform);
}
}
}
void Die()
{
Destroy(gameObject);
}
void Damage(Transform enemy)
{
//Destroy(enemy.gameObject);
}
}
**Here is the targeting system it's paired with just incase**
public class TargetingSystem : MonoBehaviour
{
private Transform target;
[Header("Attributes")]
public float range = 30f;
public float fireRate = 1f;
private float fireCountdown = 1f;
[Header("Unity Setup Fields")]
public string enemyTag = "Enemy";
public GameObject pfbullet;
public Transform firePoint;
void Start()
{
InvokeRepeating("UpdateTarget", 0f, 0.5f);
}
void UpdateTarget()
{
GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag);
float shortestDistance = Mathf.Infinity;
GameObject nearestEnemy = null;
foreach(GameObject enemy in enemies)
{
float distanceToEnemy = Vector3.Distance (transform.position, enemy.transform.position);
if (distanceToEnemy < shortestDistance)
{
shortestDistance = distanceToEnemy;
nearestEnemy = enemy;
}
}
if(nearestEnemy != null && shortestDistance <= range)
{
target = nearestEnemy.transform;
} else
{
target = null;
}
}
void Update()
{
if (target == null)
return;
if(fireCountdown <= 0f)
{
Shoot();
fireCountdown = 1f / fireRate;
}
fireCountdown -= Time.deltaTime;
}
void Shoot()
{
GameObject bulletGO = (GameObject)Instantiate(pfbullet, firePoint.position, Quaternion.identity);
Bullet bullet = bulletGO.GetComponent();
if (bullet != null)
bullet.Seek(target);
}
}
↧