Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

attack targeted enemy

Discussion in 'Scripting' started by gibberingmouther, Aug 12, 2017.

  1. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    sorry i know i post a lot here ... and i solve a lot of my own threads through the rubber duck effect ... this forum is really helping me out though and i hope i am making it easier for future developers through my solved thread content.

    "enemy" is a gameobject tag. all enemies will be tagged enemy, or so far that is the plan. attacking is a state in a finite state machine, along with approach enemy, idle, move to point, and that's all so far. attacking has an animation based on the angle of the hero to the enemy, and tests whether it hits or misses (then if it hits deals damage). pretty simple.

    so far this only works with one enemy and that is my problem. how can i make it so it "targets" only the enemy that is clicked on? so far i have no ideas but don't be surprised if i come back with an edit in like 2 minutes lol

    edit: here's some code -
    Code (csharp):
    1.  
    2. void Attack(GameObject enemy1)  // again, this just refers to one enemy. need to fix!
    3.         {
    4.             if (enemy_clicked == true)
    5.             {
    6.                 AttackEnemy(enemy1);
    7.             }
    8.         }
    9.  
     
  2. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    so far i figured out that i need to have the damage taken on a script that is on the enemy game object. i always try to bump with an update, to show that i am working on this too. i'm just stuck.
     
  3. dpgdemos

    dpgdemos

    Joined:
    Apr 28, 2014
    Posts:
    24
    I would setup an interface (e.g. IDamageable) for the enemies. If you click on the enemy and it has the tag 'enemy,' then you can check to see if they have the interface. If so, then add damage to the enemy on the enemy's script that implements the interface.
     
    gibberingmouther likes this.