S
S
Stanislav Ivanov2020-04-30 21:36:06
Unity
Stanislav Ivanov, 2020-04-30 21:36:06

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

1 answer(s)
F
freeExec, 2020-05-01
@Sterio23400

In the animation, you make an event trigger that calls the code in the component. And here you are already deleting the object in it.
m72iVsg.png?1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question