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
2
Question by xToxicInferno · Apr 23, 2010 at 01:58 PM · raycastdamagerayexplosioncover

How can I apply damage based on a grenade explosion, but test whether the objects are behind cover so they are not damaged even within the explosion radius?

I was wondering if it is possible to have a object shoot out rays in all directions. I want this for a grenade, as i want it so when the grenade blows up it shoots out rays in all directions, and if a ray hits something it will apply damage. I want to do it with ray casting because i want it so if the player is behind cover they will not get hurt, unlike if i spawned a trigger to check for collisions with player, as that would go threw walls. IF you got a better way of doing it please tell me otherwise help me the best you can. Also, can you have it set so it will calculate the length of the ray? U would like that as it would make it easier to apply damage, if the player is on top of the grenade it is instant death, within 1 meter is half health, and within 5 meters is 25% of the health. I would set the ray to be about 6 meters long if i could calculate the length of it.

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 irvintiu · Jun 09, 2017 at 11:40 AM 0
Share

@duck the script works well. the only problem is if a player is in front of another player. the player behind it would not take the damage.

how do i fix that?

also heres the code.

     float power = 100;
     int damage = 1;
 
     float radius = 100f;
 
 
     void Start()
     {
 
     }
 
     void Update()
     {
 
         if (Input.GetButton("Fire1"))
         {
             Explode();
         }
     }
 
     void Explode()
     {
 
 
         Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
         Debug.Log("Count of colliders = " + colliders.Length);
 
      
                  foreach (Collider collider in colliders)
         {
             if (collider.tag == "brick" || collider.tag == "Player")
             {
                 Debug.Log("Found brick or a player");
                 Debug.Log("collider is " + collider.name);
 
 
 
 
                 RaycastHit hit;
 
                 if (Physics.Linecast(transform.position, collider.transform.position, out hit, 5) )   // collider.tag != "Player" || collider.tag != "brick")
                 {
                    
                     if (hit.collider == collider)
                     {
                         Debug.Log("No obstructions");
 
                         if (hit.rigidbody)
                             hit.rigidbody.AddExplosionForce(power, transform.position, radius, 50f);
                      
 
 
                     }
                 }
 
          
             }
         }
     }

 

2 Replies

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

Answer by duck · Apr 23, 2010 at 03:14 PM

I would do these calculations in the opposite order:

  1. find all damagable objects within the radius
  2. cast a ray from the grenade to each found object, to test whether they are shielded

For more information, see this answer which discusses how to apply damage which falls off within a certain blast radius, and just add the raycast test to it, to check for cover.

function AreaDamageEnemies(location : Vector3, radius : float , damage : float ) { var objectsInRange : Collider[] = Physics.OverlapSphere(location, radius); for (var col : Collider in objectsInRange) { var enemy : Enemy = col.GetComponent(Enemy); if (enemy != null) { // test if enemy is exposed to blast, or behind cover: var hit : RaycastHit; var exposed = false; if (Physics.Raycast (location, (enemy.transform.position-location), hit)) { exposed = (hit.collider = enemyCollider); }

         if (exposed) {
             // Damage Enemy! with a linear falloff of damage amount
             var proximity : float = (location - enemy.transform.position).magnitude;
             var effect : float = 1 - (proximity / radius);
             enemy.ApplyDamage(damage * effect);
         }
     }
 }

}

If you're using rigidbodies for your enemy objects, you could also use AddExplosionForce to add a proximity based force to push them away from the explosion centre.

Comment
Add comment · Show 4 · 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
avatar image Eluem · Apr 03, 2016 at 08:35 PM 0
Share

I'm using a solution similar to this for explosions in a 2d game that I'm working on. I'm using a rocket of sorts that will detonate after a certain period of time, or when hitting walls/characters/ect. Upon hitting a surface, the explosion is formed. The explosion simply uses a circle collider which expands (rapidly, synced with a particle effect). When the explosion detects a collision with a character, before applying the damage and other effects, it throws a raycast that from the center of the explosion toward the detected player and checks against the high and low wall layers. If anything is found, the explosion is nullified.

This works really well.... I'm having one issue though... the rocket can often pass far enough into a wall so that the center is inside the wall, causing it to entirely block the explosion from all directions. The rocket is using continuous collision detection.. I was hoping enabling that would help.... but it didn't seem to solve the problem.

The only other solution I can think of is to come up with some cludgy way to have the explosion check if it's spawning inside a wall and push it slightly out along the shortest axis... or along an axis defined by the projectile's last and second to last positions.

Any advice?

avatar image nicmarxp · May 03, 2018 at 10:03 PM 0
Share

Great, but I think enemyCollider should be enemy on the line that starts with exposed.

avatar image mansoor090 · Oct 25, 2018 at 10:35 AM 0
Share

thanks bro, your code gave me a hint

avatar image xthunderduckx · May 24, 2019 at 05:19 PM 0
Share

I know that this is really old but something to add here, and maybe there is a work around, but location - enemy.transform.position will always raycast towards the location of the transform, because the collision points from overlap sphere are always there; So if you have even the slightest barrier blocking the center of an object, then the raycast hits that even if the enemy is clearly exposed. Is there any way to get around that?

avatar image
0

Answer by shankar_developer · May 28, 2011 at 06:40 AM

If you enable the Maximize on Play button in the Game window, the Game Window will expand when you hit play and get small again when you stop. The downside to this is that you can't see the inspector/hierarchy windows without opening them manually.

You are right in this aspect

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Detecting Raycast collision on an enemy to deal damage 1 Answer

How to damage gameobject via raycast 1 Answer

C# mouse Raycast question 3 Answers

Instantiate objects along a ray in four directions 0 Answers

Raycast returns null for no apparent reason 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