• 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 /
  • Help Room /
avatar image
Question by asalcido · Oct 06, 2022 at 05:51 AM · liststargettargetingloopstargets

Bouncing projectile targeting (Sivirs W),Bounce bullet targeting

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 :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<Transform> validTargets = new List<Transform>();
     


     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<Bullet>();

     if (bullet != null)
         bullet.Seek(target);


 }

}

Comment

People who like this

0 Show 0
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

0 Replies

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

192 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 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 avatar image avatar image

Related Questions

How do I use a spherecast to spawn multiple target images at multiple objects? (script provided) 1 Answer

Targeting Script Errors 0 Answers

Cant loop through a List 2 Answers

Seemingly easy Targetting problem! 0 Answers

Switching Target 0 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