How i can damage more than one enemy?
This is my script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AtaqueMeleePlayer : MonoBehaviour {
public GameObject enemies;
public float tiempoDeAtaque;
public float cooldDown;
// Use this for initialization
void Start () {
tiempoDeAtaque = 0;
}
// Update is called once per frame
void Update () {
enemies = GameObject.FindWithTag ("Enemy");
if (tiempoDeAtaque > 0)
tiempoDeAtaque -= Time.deltaTime;
if (tiempoDeAtaque < 0)
tiempoDeAtaque = 0;
if(Input.GetKeyDown(KeyCode.Mouse0)){
if (tiempoDeAtaque == 0) {
Attack ();
tiempoDeAtaque = cooldDown;
}
}
}
private void Attack (){
float distance = Vector3.Distance (enemies.transform.position, transform.position);
Vector3 dir = (enemies.transform.position - transform.position).normalized;
float direction = Vector3.Dot (dir, transform.forward);
//Debug.Log (direction);
//Debug.Log (distance);
if (distance < 3) {
if (direction > 0) {
//VidaEnemigo eh = (VidaEnemigo)enemies.GetComponent ("VidaEnemigo");
//eh.AjustarVidaActual (-10);
VidaSkeleton a = (VidaSkeleton)enemies.GetComponent ("VidaSkeleton");
a.AjustarVidaActual (-10);
}
}
}
}
Firstly i want to detect all the enemies in a range of 20u. And second i want to damage all the enemies that they are in of if (distance < 3) { if (direction > 0) {
Thank you very much, i was attempting but doesn't works
simple way you need array of objects GameObject [] enemies;
& then use enemies= GameObject.FindGameObjectsWithTag("Enemy");
& then use a loop to loop throw the array i will give you simple example :
GameObject [] enemies;
void Start(){
enemies = GameObject.FindGameObjectsWithTag("Player");
if (enemies != null) {
for (int i = 0; i < enemies.Length; i++) {
float distance = Vector3.Distance (enemies [i].transform.position, transform.position);
print (distance);
}
}
}
I have another method but it works. Thanks you
Answer by M-Hanssen · Apr 13, 2016 at 03:01 PM
Take a look at the overlapSphere method
I've fixed you an example by using your code:
public float AttackDistance = 3;
public float DetectionDistanceDistance = 20;
protected void Attack()
{
// Detect all enemies within a radius
foreach (Collider hitCollider in Physics.OverlapSphere(transform.position, DetectionDistanceDistance))
{
VidaSkeleton skeleton = hitCollider.GetComponent<VidaSkeleton>();
if (skeleton != null)
{
// Add a check if the collider
Debug.Log(hitCollider.name + " is detected");
}
}
// Damage enemies within a radius
foreach (Collider hitCollider in Physics.OverlapSphere(transform.position, AttackDistance))
{
VidaSkeleton skeleton = hitCollider.GetComponent<VidaSkeleton>();
if (skeleton != null) skeleton.AjustarVidaActual(-10);
}
}
It works, thanks you, but i'm not totally unsertood the overlapSphere method. If you can explain to me i will be very grateful with you. If not dont worry, :P.
Thank you mate.
The overlapSphere method puts a sphere around the chosen position and detects all colliders living inside that sphere. As simple as that ;-) The method itself returns a collection of colliders where you can loop through.
Your answer
![](https://koobas.hobune.stream/wayback/20220612074746im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to get my TakeDamage script to hit/get other components? 0 Answers
Expanding Ring Attack 0 Answers
Targetting enemy, repeating code 1 Answer
Range Attack + Animation Script 0 Answers
Charecter keeps playing walking animation after punching 0 Answers