- Home /
no longer need answer
What would be the best way to do something based on the animation state of the character?
So quite simply, I have an enemy that has two states, idle and attack, with an animation for each state.
I want that all the time the enemy is in the idle state it should play a "weeping" audio clip on loop, and when I get near and the attack condition and animation is activated it should play the "scream" audio clip.
I just dont seem to get this to work, some guidance would be appreciated.
This is what I'm using to control the states of the enemy, I dont know how to implement the sound clip in here based on what animation state they are in.
public class EnemyAI : MonoBehaviour
{
public UnityEngine.AI.NavMeshAgent agent { get; private set; } // the navmesh agent required for the path finding
public ThirdPersonCharacter character { get; private set; } // the character we are controlling
public Transform target; // target to aim for
public float dangerRadius = 5f;
private void Start()
{
// get the components on the object we need ( should not be null due to require component so no need to check )
agent = GetComponentInChildren<UnityEngine.AI.NavMeshAgent>();
character = GetComponent<ThirdPersonCharacter>();
agent.updateRotation = false;
agent.updatePosition = true;
}
private void Update()
{
if (target != null)
agent.SetDestination(target.position);
if (agent.remainingDistance < dangerRadius)
{
agent.isStopped = false;
character.Move(agent.desiredVelocity, false, false);
}
else
{
character.Move(Vector3.zero, false, false);
agent.isStopped = true;
}
if (agent.remainingDistance < 1f)
{
//maybe call attack animation, or scream animation
Debug.Log("you died bro");
}
}
public void SetTarget(Transform target)
{
this.target = target;
}
}
}
Follow this Question
Related Questions
Hello! I have some issues with animations. 1 Answer
Cant get Animator Controller to work properly 2 Answers
Why aren't my parameters for animation not working? 0 Answers
How to prevent an animation from triggering more than on due to fast clicking? 2 Answers
How to get back to the first position animatedly in unity? 1 Answer