- Home /
Make a damage in area from a shoot.
Hey there everyone.
I've got the last Unite 2014 tutorial, where you make a simple shooter with bunnies. I'm trying to make that the shoot, when hit an enemy, it does massive damage around, in a sphere area, but I can't seem to figure it out.
I have this function on player:
void Shoot ()
{
timer = 0f;
gunAudio.Play ();
gunLight.enabled = true;
gunParticles.Stop ();
gunParticles.Play ();
gunLine.enabled = true;
gunLine.SetPosition (0, transform.position);
shootRay.origin = transform.position;
shootRay.direction = transform.forward;
if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
{
EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
if(enemyHealth != null)
{
enemyHealth.TakeDamage (damagePerShot, shootHit.point);
}
gunLine.SetPosition (1, shootHit.point);
}
else
{
gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
}
}
And in enemy, he receives damage with:
public void TakeDamage (int amount, Vector3 hitPoint)
{
if(isDead)
return;
enemyAudio.Play ();
currentHealth -= amount;
hitParticles.transform.position = hitPoint;
hitParticles.Play();
if(currentHealth <= 0)
{
Death ();
}
}
Can anyone help me with that?
Answer by AlwaysSunny · Oct 28, 2014 at 05:09 AM
Have a look at Physics.OverlapSphere(), that's probably what you'll wanna use.
Umh.. I may be going in the wrong way.
I tried
if (Physics.CheckSphere(shootHit.point, sphereRadius))
{
enemyHealth.TakeDamage (damagePerShot, shootHit.point);
}
Sorry, did say something wrong. Actually it just don't make an area of range effect. It has nothing to do with enemies in a circle, that is another thing I've done.
Physics.CheckSphere will return a list of colliders that touch the sphere described by the arguments. You then check each collider for an Enemy component, and only try calling TakeDamage on found components.
Right, this is a little bit complex for me. I got the main idea. This code will check if there are enemies around the sphere. I need to select witch colliders are enemies and then apply TakeDamage on each collider that fits the description.
I just can't figure it out how to translate this to a code. :(
Sorry, I was mistaken earlier. Edited my answer. You want Physics.OverlapSphere(), which returns an array of colliders. iterate over the array, checking each collider for the desired componenet. If it's not null, call TakeDamage() on it.
http://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html
Just tried and worked! :)
Thank you so much!
In case anyone want the code as referece, here it is:
if(Physics.Raycast (shootRay, out shootHit, range, shootable$$anonymous$$ask))
{
Collider[] hitColliders = Physics.OverlapSphere(shootHit.point, radius);
int i = 0;
while (i < hitColliders.Length) {
if (hitColliders[i].tag == "Zombunny" || hitColliders[i].tag == "Zombear" || hitColliders[i].tag == "Hellephant")
{
Debug.Log("Hit: " + hitColliders[i].name);
EnemyHealth enemyHealth1 = hitColliders[i].GetComponent<EnemyHealth>();
if (enemyHealth1 != null)
{
enemyHealth1.TakeDamage(damagePerShot, shootHit.point);
}
}
i++;
}
EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
if(enemyHealth != null)
{
enemyHealth.TakeDamage (damagePerShot, shootHit.point);
}
gunLine.SetPosition (1, shootHit.point);
}
else
{
gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
}
Your answer
Follow this Question
Related Questions
Sidescroll Shooter Crosshair? 0 Answers
Weapon pick up 3 Answers
Gun Wont Follow Camera Up and Down With Unity's First Person Controller 2 Answers
Altering Material Alpha While Raycast is NOT Hitting 0 Answers
Awareness script doesnt work 0 Answers