Answer the question
In order to leave comments, you need to log in
How to set the trigger time of a function?
Good day to all. I practiced writing a script to attack an enemy when the hero is at a certain distance from the enemy. There is also an attack attack animation, everything works, but the fact is that the destruction of the hero occurs independently of the animation, that is, the hero can be destroyed before the desired moment of the animation happens. So here's how to make the necessary delay in the function so that the destruction is carried out exactly at the right moment of the strike animation (in this case, when the enemy's arm straightens in the strike). The entire script is shown below. I have also attached a link to a video that clearly shows the issue where the hero is destroyed before the enemy hit animation.
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using UnityEngine;
public class Enimy_attack : MonoBehaviour
{
public Transform attackPose;
public float attackRange;
public LayerMask whatIsEnymy;
private Transform target;
private Animator animator;
void Start()
{
animator = GetComponent();
target = GameObject.FindGameObjectWithTag("Player").GetComponent();
}
void Update()
{
if (Atack())
{
Collider2D[] HeroToDestroy = Physics2D.OverlapCircleAll(attackPose.position, attackRange, whatIsEnimy);
for (int i = 0; i < HeroToDestroy.Length; i++)
{
HeroToDestroy[i].GetComponent().Damage();
}
}
}
bool Attack()
{
if (Vector2.Distance(transform.position, target.position) < 6)
{
animator.SetTrigger("Dead");
return true;
}
else
{
return false;
}
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(attackPose.position, attackRange);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question