- Home /
 
Enemy attack target
How do i make so when the enemy comes within a attack radius its damaging the target there is 2 different targets there is a building and there is a player so how do i make it attack the target there is within attack radius this is the script
 void Update()
 {
     TargetFinder();
     if (currentTargetDistance < attackRadius)
         Attack();
     if (myTarget == null)
         currentTargetDistance = searchRadius;
     else
         Move(myTarget.transform);
     if (CurrentHealth < DeathHealth)
     {
         print("Enemy Has died!!!");
         Destroy(GroundEnemy);
     }
     print("Enemy took some damage");
 }
 void TargetFinder()
 {
     foreach (var x in Targets)
     {
         float distance = (transform.position - x.transform.position).magnitude;
         if (distance < currentTargetDistance)
         {
             myTarget = x;
             displayText.text = "My target: " + x + "\n Distance to Target: " + distance; //skal fjernes nå det er helt klart
             currentTargetDistance = distance;
         }
     }
 }
 void Move(Transform t)
 {
     transform.LookAt(t);
     transform.position += transform.forward * MoveSpeed * Time.deltaTime;
 }
 void Attack()
 {
     Targets.Remove(myTarget);
     Destroy(myTarget);
     myTarget = null;
     currentTargetDistance = searchRadius;
 }
 
              Answer by camdenlink7 · Jun 17, 2019 at 02:25 PM
Hi, one of the problems may be that your top two if statements do not have curly brackets. However, I would recommend a different approach to the whole thing. I would put a collider on your player (you can change the size to your liking) as the radius. Make sure it is checked as a trigger, and then use
  void OnTriggerEnter(Collision col)
  {
      if (col.gameObject.name == ("Enemy's name")
          {
              //Attack code
          }
  }
 
               Put this code directly on your player and don't forget to put the collider on. I hope any of this helps!
The thing is there is going to be slot og building and maybe 4 players so how do I do so it find the script of the specific taget and da age that one?
To find the script on an object, you can do
 private scriptName script;
 
 void Start()
  {
      script = objectScriptIsOn.GetComponent<scriptName>();
  }
 
 
                 what is the best replacement here for raycasthit?
    void DoAttack()
     {
         if (hit.collider.tag == "Player")
         {
             PlayerController eHealth = hit.collider.GetComponent<PlayerController>();
             PlayerController.instance.CurrentHealth -= Random.Range($$anonymous$$WeaponDamage, maxWeaponDamage + 1);
             print("Player took some damage");
         }
         myTarget = null;
         currentTargetDistance = searchRadius;
     }
                  Your answer
 
             Follow this Question
Related Questions
Need Help With My AI Script 1 Answer
Attack/Targeting Script Issue 1 Answer
Is this the most efficient way to attack the closest enemy? 0 Answers
Attacking enemy script problem 0 Answers
How to rig an enemy? 1 Answer