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 H1ddeNGames · Aug 15, 2017 at 09:50 AM · c#physicsraycast

Extending Survival Shooter Tutorial Using RaycastAll to attack all enemies in a line.

Preface: I started with the Survival Shooter tutorial and added a ton of stuff. Here I'm trying to add a power-up that will shoot through every enemy in a line. I have put in a counter so later on when I want to tweak the power-ups I can select how many enemies should be shot through.

The problem: In the game and scene view I can see the Raycast (in the form of a line render implemented in the tutorial) pass through multiple enemies or even single ones and not return that it has hit. I have tested it by playing the game and shooting from different angles and ranges. It seems that the Raycast will hit the enemy about 50% or less of the time. This is in the PlayerShooting script.

The tutorial's working code in C# that hits only 1 enemy at a time 100% of the time:

 if (Physics.Raycast(shootRay, out shootHit, range, shootableMask))
         {
             //Debug.Log(shootHit.transform.name);
             //Debug.Log(shootHit.transform.position);
             // Get the enemy health script in order to apply damage.
             // The thing that has been hit might not be an enemy.
             EnemyHealth enemyHealth = shootHit.collider.GetComponent<EnemyHealth>();
 
             // So first check if the script is empty or not, otherwise an error or crash will occur.
             if (enemyHealth != null)
             {
                 damagePerShot = Random.Range(damageMin, damageMax);
                 //Debug.Log(damagePerShot);
                 //Debug.Log(enemyHealth.transform);
 
                 // Damage done to an enemy causes the amount of damage done to be displayed on the canvas. 
                 FloatingTextManager.CreateFloatingText("-" + damagePerShot.ToString() + " HP", enemyHealth.transform);
                 enemyHealth.TakeDamage(damagePerShot, shootHit.point);
             }
             // End the line render when the line hits an object.
             gunLine.SetPosition(1, shootHit.point);
         }
         else
         {
             // Draw a line that's 100 units.
             gunLine.SetPosition(1, shootRay.origin + shootRay.direction * range);
         }

Testing non-working code in C# that hits all enemies in a line <50% of the time:

 RaycastHit[] hits = Physics.RaycastAll(shootRay, range, shootableMask);
         
         int i = 0; 
         while(i < hits.Length)
         {
             //Debug.Log(hits[i].transform.name);
             EnemyHealth enemy = hits[i].transform.GetComponent<EnemyHealth>();
             //Debug.Log(enemy);
             Debug.Log(hits[i].point);
             if (enemy != null)
             {
                 counter += 1;
                 gunLine.SetPosition(1, hits[i].point);
                 enemy.TakeDamage(damageMin, hits[i].transform.position);
             }
             else
             {
                 gunLine.SetPosition(1, shootRay.origin + shootRay.direction * range);
                 return; 
             }
             i++;
         }
         Debug.Log(counter);
         counter = 0;

If you need more information or the entire script, please let me know. I've tried for a couple hours and nothing I've looked up or tried has worked.

Comment
Add comment · Show 1
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 Iceman320 · Sep 12, 2017 at 12:40 PM 0
Share

I am looking for an answer to the same question, as i was also trying to make power ups that enable you to shoot through multiple enemies.

Have managed to make the power up pickups spawn at certain times, but am having the same issue as you where i don't know how to essentially "Beam" the Ray cast through all enemies in a line and do damage to all of them.

If you figure it out, please advise :)

1 Reply

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

Answer by PizzaPie · Sep 13, 2017 at 07:46 AM

A basic ,pretty much what you did, approach would be something like this:

 RaycastHit[] hits = Physics.RaycastAll(shootRay, range, shootableMask);
 
             if (hits.Length != 0)      //might need (hits != null && hits.Length !=0) don't remember if it returns empty array or null
             {
                 foreach (RaycastHit hit in hits)    //may be replaced with for loop
                 {
                     EnemyHealth enemyHealth = hit.collider.GetComponent<EnemyHealth>();
                     if (enemyHealth != null)
                     {
                         enemyHealth.TakeDamage(100, hit.point);
                     }
                     gunLine.SetPosition(1, hit.point);
                 }
             }
             else
             {
                 gunLine.SetPosition(1, shootRay.origin + shootRay.direction * range);
             }

Don't use a while loop when you can use for /foreach loops. But that "solution" has a major bug it will ignore bolders. That s due to the fact that the hits array's elements are in "random" order.

To achieve a proper solution some "easy" ways would be

  1. pick up the raycast result array sort it according to distance and then iterate though it, damage all objects till you reach a non-enemy one (emenyHealth == null) then stop the process. In this solution you can apply aslo consecutive damage reduction.

  2. pick up the raycase array find the closest bolder element (enemyHealth== null/ or with tags) store the distance re iterate through the array kill all enemies with distance lower than the stored one. With this you may not apply concecutive dmg reduction as you don't know the order of the elements.

In general the first will give you better control of the situation but its performance highly depends on the sorting algorithm you ll use, the second one might be more performance friendly as it will iterate only twice on any senario but won't allow special behaviours to applied. Cheers!

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

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

Vector3.Movetoward. Why does this work? 0 Answers

AI Shooting for Aircraft 1 Answer

Multiple Cars not working 1 Answer

using A* pathfinding to go to player after raycast has seen it - c# 0 Answers

Change the direction of ScreenPointToRay() Ray 1 Answer


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