- Home /
Make grenade doesnt affect anything if it is behind wall
Hello guys, its my first post here, hope you can help me... I have a grenade object, that destroys itself in a certain time, and I have THIS FUNCTION CALLED IN OnDestroy() :
function AreaDamageEnemies(location : Vector3, radius : float , damage : float ) {
var objectsInRange : Collider[] = Physics.OverlapSphere(location, radius);
for (var hit : Collider in objectsInRange) {
if (!hit) {
continue;
}
var proximity : float = (location - hit.transform.position).magnitude;
var effect : float = 1 - (proximity / radius);
if (hit.tag == "Movible") {
hit.rigidbody.AddExplosionForce(50, this.transform.position, 10, 3.0);
}
if(hit.tag == "Blanco") {
var VidaNegra : ManejadordeBlancos = hit.GetComponent(ManejadordeBlancos);
var IA : InteligenciaArtificialNegra1 = hit.GetComponent(InteligenciaArtificialNegra1);
var NuevoVector : Vector3 = hit.transform.position - this.transform.position;
VidaNegra.DecrementaSalud(damage * effect);
IA.ExplosionGranada(NuevoVector);
}
if(hit.tag == "Vidrio") {
hit.GetComponent("VidaVidrios").VidaVidrio = 0;
}
if(hit.tag == "Player") {
var Vida : playerStatus = hit.GetComponent(playerStatus);
var varx = ((damage * effect) - 20);
if(varx > 0)
Vida.aplicarDamage(varx);
}
}
}
You dont have to fully understand what I do to every single tag, I talk spanish and program in spanish, so you wont understand most names, I just wants those "if´s" to be executed if its not a wall in the way... I tried all the ways, I searched "Grenade damage" to page 5 of the forums but nothing will work, It must be me who is doing it wrong, can anybody help me?
Have you tried using a raycast that starts in the grenade position and end in the tagged object (player, blanco, etc)? http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
Is it possible that the wall is also a rigidbody, and the explosion is only absorbed by the wall, without spash damage affecting other objects within the radius?
Sorry if it sounds quite noob but I just cant get the raycast done, I mean, from all the versions of the Raycast function they ask an Origin, and a direction, What is the way to represent that "Direction" in to the position of the enemy? I think thats where my error is but I cant get it right! Can you tell me what the parametres of the raycast would be?
Ok, I added
var COL : RaycastHit;
if (Physics.Raycast (location, (hit.transform.position - location), COL))
At the beginning of the for (var hit : Collider in objectsInRange) and encapsuled all the "if´s" in that Raycast, and in the "if´s" checks I do, for example this:
if(hit.tag == "Player" && COL.collider.tag == "Player")
But the surprise I got, is the grenade works only with the player, I mean, the only one who gets hurted is the player, not the enemy (Blanco) or the glasses (Vidrio) Any idea why is this?
Note: The player gets hurted only when is directly exposed to the grenade, not if there is a wall between, so basically the script works well, but only with the player... :(
Answer by Scacchi · Feb 17, 2013 at 04:10 AM
Guys, I found the answer and it was the simpliest thing ever done, since its indeed the answer to the question Im going to post it here... Aparently when you use raycast, and you tell it to go from the position of one object (Grenade) to the position of another object (Enemy, Player, etc) the Raycast goes FROM PIVOT POINT ---> TO PIVOT POINT So my problem was this:
I had my pivot point of the object OUTSIDE of the collider, so, the raycast goes from the grenade pivot point to the enemy pivot point, did it hit anything? No, because the pivot point isnt inside the collider, so it never touched it... So I did what you see in the second example... I expanded the CharacterController (It could be any collider) so the pivot point its inside it, so the raycast will hit it. Fortunately for me, I was going to change the model of the enemy for one better shaped, so I dont really care, Im just going to make sure the pivot point of this one is in the center, or the head, so I make sure there wont be colliders errors... Hope you dont get any serious problem with this, good luck and thanks for helping!
Your answer
![](https://koobas.hobune.stream/wayback/20220613092728im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycast grenade? 2 Answers
C# Grenade Explosion in Radius 1 Answer
Grenade Damage in Radius 2 Answers
Deal Damage On Collision 3 Answers
[SOLVED]Different levels of damage per particle collision 1 Answer