i tried to get my soldier to attack enemy soldier when they are in range. but they only attack one o

i tried to get my soldier to attack enemy soldier when they are in range. but they only attack one or twice. then they just stand there .please help me, i tried everything i could find still cant fix it.

csharp** **using System.Collections;[/B] [B]using System.Collections.Generic;[/B] [B]using System.Linq;[/B] [B]using UnityEngine;[/B] [B]using UnityEngine.UIElements;[/B] [B][/B] [B]public class soldier : MonoBehaviour[/B] [B]{[/B] [B] public int maxHealth;[/B] [B] public int CurrentHealth;[/B] [B] public int damage;[/B] [B] public int armor;[/B] [B][/B] [B] public float fightDistance;[/B] [B] public float attackRadius;[/B] [B] public float nextAttackTime = 1;[/B] [B][/B] [B] public GameManager gamemanager;[/B] [B][/B] [B] public GameObject player;[/B] [B][/B] [B] enemySoldier closestEnemy = null;[/B] [B] void Start()[/B] [B] {[/B] [B] player = GameObject.FindGameObjectWithTag("player");[/B] [B] gamemanager = FindObjectOfType<GameManager>();[/B] [B][/B] [B] this.GetComponent<Animator>().SetTrigger("walking");[/B] [B][/B] [B][/B] [B] maxHealth = gamemanager.allyHealth ;[/B] [B] CurrentHealth = maxHealth;[/B] [B] }[/B] [B][/B] [B] // Update is called once per frame[/B] [B] void Update()[/B] [B] {[/B] [B][/B] [B] if(CurrentHealth <= 0)[/B] [B] {[/B] [B] Destroy(this.gameObject);[/B] [B] }[/B] [B] damage = gamemanager.allydamage;[/B] [B] armor = gamemanager.allyarmor;[/B] [B][/B] [B][/B] [B][/B] [B] FindClosestEnemy();[/B] [B][/B] [B][/B] [B] if (closestEnemy != null && Vector2.Distance(closestEnemy.transform.position, this.gameObject.transform.position) <= fightDistance)[/B] [B] {[/B] [B] Vector2 newPos = closestEnemy.transform.position;[/B] [B] this.gameObject.transform.position = Vector2.MoveTowards(this.transform.position, newPos, Time.deltaTime);[/B] [B] }[/B] [B] else[/B] [B] {[/B] [B] Vector2 newPos = player.transform.position;[/B] [B] this.gameObject.transform.position = Vector2.MoveTowards(this.transform.position, newPos, Time.deltaTime);[/B] [B] }[/B] [B][/B] [B] if(Vector2.Distance (closestEnemy.transform.position ,this.gameObject .transform .position )<= attackRadius && Time.time >= nextAttackTime)[/B] [B] {[/B] [B][/B] [B] this.GetComponent<Animator>().SetTrigger("attacking");[/B] [B][/B] [B] closestEnemy.GetComponent<enemySoldier>().CurrentHealth -= (damage - closestEnemy.GetComponent<enemySoldier>().armor);[/B] [B][/B] [B] nextAttackTime += Time.time;[/B] [B] }[/B] [B][/B] [B] }[/B] [B][/B] [B] public void FindClosestEnemy()[/B] [B] {[/B] [B] float distanceToClosesetEnemy = Mathf.Infinity;[/B] [B] [/B] [B] enemySoldier[] allEnemies = FindObjectsOfType<enemySoldier>();[/B] [B][/B] [B] foreach(enemySoldier currentEnemy in allEnemies)[/B] [B] {[/B] [B] float distanceBetweenThisAndEnemy = Vector2.Distance(currentEnemy.transform.position, this.gameObject.transform.position);[/B] [B] if(distanceBetweenThisAndEnemy <distanceToClosesetEnemy)[/B] [B] {[/B] [B] distanceToClosesetEnemy = distanceBetweenThisAndEnemy;[/B] [B] closestEnemy = currentEnemy;[/B] [B] }[/B] [B] }[/B] [B] }[/B] [B][/B] [B] public void OnDrawGizmos()[/B] [B] {[/B] [B] Gizmos.DrawWireSphere(this.gameObject.transform.position, fightDistance);[/B] [B] Gizmos.DrawWireSphere(this.gameObject.transform.position, attackRadius);[/B] [B] Gizmos.DrawWireSphere(this.gameObject.transform.position, Vector2.Distance(closestEnemy.transform.position, this.gameObject.transform.position));[/B] [B] }[/B] [B]}[/B] [B]** **

I see you have some good debugging already going on with OnDrawGizmos. That should help you find the problem.

I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

Here are some other ideas to help track down the eissue:

  • set up a new custom simple scene, the minimum required to prove the problem (one enemy one player?)
  • reduce Time.timeScale to a smaller value to slow the world down (such as 0.25 or 0.1) so you can see whats going on.
  • keep the Scene window open alongside the Game window to watch your gizmos.
1 Like

thx I think i got the problem, after what u said i went back and put some debuglog like that.

Debug.Log("close enemey distance: " + Vector2.Distance(closestEnemy.transform.position, this.gameObject.transform.position) +"\nattack Radius: " + attackRadius );
        Debug.Log("time now" + Time.time + "attack time" + nextAttackTime);

then i found my line code line 69 is where it went wrong. when next attack time =+time.time its more like multiply the time it attack.
so first attack time is 1 and then it became 2 and then 4 ,8 ,16…

1 Like

Hey and welcome. You seem to be new to forums in general, so please consider these in future posts:

  • Please dont randomly change your font size for the entire post
  • Please dont write your entire post in bold… or italic, underline, … or pink for that matter
  • Please dont try to format code, as that does not work inside the code tags
  • Please try to find a short and descriptive title. Dont just try to put your entire post there.

Posts that are easy to read and contain appropriate amounts of information and effort put into them often get more and better replies. Glad you got your problem sorted out tho :slight_smile:

got it!
XD

1 Like