Search Unity

Enemy attacked to player

Discussion in 'Scripting' started by Timoty75, Sep 24, 2018.

  1. Timoty75

    Timoty75

    Joined:
    May 29, 2017
    Posts:
    150
    Good morning to everybody, is happening me a big problem:
    I have in the scene an enemy (with artificial intelligence) and the principal character. The script works that the character when it draws near to the enemy, this last follows him and it starts to fight. Once that the principal character defeats the enemy, part the animation die (of the enemy). Once that the enemy dies, it happens that the same, follows the character for then to stay to a certain distance, he stopped...why?
    Thank a lot!
    Perhaps there is a problem in this function?: if (healthbar.value <= 0) return;

    Slider:
    Code (CSharp):
    1. using
    2.  
    3. [code=CSharp]using System.Collections;
    4. using UnityEngine.UI;
    5. using UnityEngine;
    6. using System;
    7. public class guardarecoboldo2 : MonoBehaviour
    8. {
    9.  
    10.     public Transform player;
    11.     Animator anim;
    12.  
    13.     public Slider healthbar;
    14.  
    15.     void Start()
    16.     {
    17.  
    18.  
    19.         anim = GetComponent<Animator>();
    20.  
    21.     }
    22.     void Update()
    23.     {
    24.  
    25.  
    26.  
    27.  
    28.         if (healthbar.value <= 0) return;
    29.  
    30.         if (Vector3.Distance(player.position, this.transform.position) < 10)
    31.         {
    32.             Vector3 direction = player.position - this.transform.position;
    33.             direction.y = 0;
    34.  
    35.             this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
    36.                 Quaternion.LookRotation(direction), 0.1f);
    37.  
    38.             anim.SetBool("Idle", false);
    39.             if (direction.magnitude > 2
    40.  
    41.  
    42.  
    43.  
    44.                 )
    45.             {
    46.                 this.transform.Translate(0, 0, 0.05f);
    47.                 anim.SetBool("Walk", true);
    48.                 anim.SetBool("Attack", false);
    49.             }
    50.             else
    51.             {
    52.                 anim.SetBool("Attack", true);
    53.                 anim.SetBool("Walk", false);
    54.             }
    55.         }
    56.         else
    57.         {
    58.             anim.SetBool("Idle", true);
    59.             anim.SetBool("Walk", false);
    60.             anim.SetBool("Attak", false);
    61.         }
    62.     }
    63. }
    64.  
    65.  
    66.  
    67.  
    68.  
    69.  
    70.  
    Enemy AI:


    System.Collections;
    using UnityEngine;
    using UnityEngine.UI;

    public class slider : MonoBehaviour {


    public Slider healthbar;
    Animator anim;
    public GameObject door;
    private object theCollision;
    internal static float value;

    void OnTriggerStay(Collider theCollision)
    {



    if (theCollision.gameObject.tag == "Sword")
    healthbar.value -= 10;


    if (theCollision.gameObject.tag == "Arrow")
    healthbar.value -= 2;

    if (theCollision.gameObject.tag == "Mace")
    healthbar.value -= 13;
    if (theCollision.gameObject.tag == "Axe")
    healthbar.value -= 15;

    if (theCollision.gameObject.tag == "Dagger")
    healthbar.value -= 5;

    if (theCollision.gameObject.tag == "Magic")
    healthbar.value -= 6;



    if (healthbar.value <= 0)
    anim.SetBool("Die", true);

    }



    void Start()
    {
    anim = GetComponent<Animator>();

    }
    private void Update()
    {

    }
    }

    [/code]
    ? (function RETURN) I Attach the script of the artificial intelligence and the slider of the enemy. thanks to everybody.