Search Unity

Enemy pursue after hit!

Discussion in 'Scripting' started by henmachuca, Nov 1, 2016.

  1. henmachuca

    henmachuca

    Joined:
    Oct 14, 2016
    Posts:
    105
    Hello, I am starting a spell system and as I was testing I figured out something.

    My enemies have a detect range that I specify, but some spells casting range are higher then the enemies detect range.
    So with some spells I can hit the enemy until he dies and he will never pursue me.

    What would be a good way of making the enemy pursuing my player after being hit by the spell?
     
  2. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    I would do something like this:
    Code (CSharp):
    1. void OnColliderEnter(Collider col) {
    2.     if(col.tag == "spell") { //tag the spell projectiles as 'spell'
    3.         //move the enemy towards the player
    4.     }
    5. }
    If you put this on the enemy script, once a projectile with the tag "spell" hits it, you can call the movement method (or whatever code you use to move the enemy). Let me know if you have any more questions.
     
    henmachuca likes this.
  3. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    292
    I use a function to damage the objects in my current project, and this function takes next to the amount and the type of damage a reference to the object that was the source of it as parameters.
     
  4. henmachuca

    henmachuca

    Joined:
    Oct 14, 2016
    Posts:
    105
    Hey @Piflik , what parameters?! Can you show the function?!
     
  5. absolute_disgrace

    absolute_disgrace

    Joined:
    Aug 28, 2016
    Posts:
    253
    Do you use a boolean to change states to show whether the enemy is in pursue mode? Could you use the col.tag from earlier and simply have it set this pursue flag to true?

    If not, what happens if your player moves inside the detect range, then manages to leave the detect range? Does your enemy stay in pursuit or stop?
     
    henmachuca likes this.
  6. henmachuca

    henmachuca

    Joined:
    Oct 14, 2016
    Posts:
    105
    I don't have a bool but I have a range check to engage the enemy to the pursue mode. If I enter the range and leave after the enemy goes back to wander around!
    I think I can try to override this with a bool state of pursue whenever the mob gets hit, turning it to true and making the enemy pursue the player regardless the range and whenever the enemy attacks it set this bool to false again, returning it to its normal behavior.

    Do you think that could work?
     
  7. absolute_disgrace

    absolute_disgrace

    Joined:
    Aug 28, 2016
    Posts:
    253
    I'd set it up such that you do a check if the player has entered the engagement range, and if so set the boolean to true. Your pursue code can then completely ignore everything except the pursue boolean. Since its entirely focused on the boolean, it doesn't care why it was set, it only cares if it is set. This separation allows you the flexibility to have multiple ways to cause your enemies to engage in pursuit.

    You could do an additional check if they cross another larger range of when to disengage (set it to false). Dark Souls, as an example, has the engagement range based on the enemies current position but has the disengage range based on the level layout itself (and a limit on where the enemies will move to).
     
  8. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    292
    Sure. This is the signature of my function (I have defined this in an interface which anything that should be damaged has to implement):

    Code (CSharp):
    1. int Damage(Character damageSource, int amount, Weapon.DamageType damageType, int apPower, bool ignoreArmor);
    Nothing special, really. Character is a custom class that is central to my character system. I use the reference to give the character, who dealt the damage, experience relative to the damage dealt, but you can also use it to make the damaged entity aware of the attacker.