Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

My enemy does not attack well

Discussion in 'Animation' started by llamaverde, Nov 18, 2021.

  1. llamaverde

    llamaverde

    Joined:
    Jun 18, 2021
    Posts:
    5
    Hello, I have this script for an enemy to attack a character, but the problem is that he attacks him once and when the attack animation ends he stays still and does nothing, could someone tell me if the script has any flaws?

    Apart from that it does not show me the print "Daño" in the console.

    Thanks.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Enemigo1 : MonoBehaviour
    {
    public int rutina;
    public float cronometro;
    public Animator animador;
    public Quaternion angulo;
    public float grado;

    public GameObject target;
    public bool atacando;

    void Start()
    {
    animador = GetComponent<Animator>();
    target = GameObject.Find("Personaje");
    }

    public void ComportamientoEnemigo()
    {
    if (Vector3.Distance(transform.position, target.transform.position) > 10)
    {
    animador.SetBool("correr", false);
    cronometro += 1 * Time.deltaTime;
    if (cronometro >= 4)
    {
    rutina = Random.Range(0, 2);
    cronometro = 0;
    }
    switch (rutina)
    {
    case 0:
    animador.SetBool("andar", false);
    break;

    case 1:
    grado = Random.Range(0, 360);
    angulo = Quaternion.Euler(0, grado, 0);
    rutina++;
    break;

    case 2:
    transform.rotation = Quaternion.RotateTowards(transform.rotation, angulo, 0.5f);
    transform.Translate(Vector3.forward * 1 * Time.deltaTime);
    animador.SetBool("andar", true);
    break;
    }
    }
    else
    {
    if (Vector3.Distance(transform.position, target.transform.position) > 1 && !atacando)
    {
    var lookPos = target.transform.position - transform.position;
    lookPos.y = 0;
    var rotation = Quaternion.LookRotation(lookPos);
    transform.rotation = Quaternion.RotateTowards(transform.rotation, rotation, 2);
    animador.SetBool("andar", false);

    animador.SetBool("correr", true);
    transform.Translate(Vector3.forward * 2 * Time.deltaTime);

    animador.SetBool("atacar", false);
    }
    else
    {
    animador.SetBool("andar", false);
    animador.SetBool("correr", false);

    animador.SetBool("atacar", true);
    atacando = true;
    }

    }
    }

    public void FinalAtaque()
    {
    animador.SetBool("atacar", false);
    atacando = false;
    }
    void OnTriggerEnter(Collider collider)
    {
    if (collider.CompareTag("Arma"))
    {
    print("Daño");
    }
    }
    void Update()
    {
    ComportamientoEnemigo();
    }
    }