Triggering animation and attack state from our code

This topic covers the few lectures where we are adding code to our trigger our animation states and ultimately having the enemy damage the player. Any thoughts or questions?

Maybe this is new with Unity 2019.x (I’m in 2019.2) but I noticed now you can access private methods in Animation Events where previously the needed to be public. I don’t know if there are set backs to this but you can also see in Rick’s video he has access to all of the methods within EnemyAI.cs.

At the very least it lets your protect your code a little better.


Hi all, first time posting on here. Been having a great time learning Unity with these courses. Big thank you to Rick and Ben for making these so interesting and engaging.

I’ve run into a bit of a problem here and not sure what is causing it. Only noticed it now, so could be linked to the work done with the animations, but not sure.

For some reason, my enemy doesn’t chase any more. It didn’t have this problem before and I’m not sure when it started, but all it does is turn to face the player, without moving from its spot. I did set an animation for the movement (basically, similar to the idle one done as a placeholder in the videos, but made to be like the enemy was bouncing towards you), so I tried commenting out the line of code to transition to this animation, but that didn’t have any effect. I’ve also added a line to print the target.transform.position to console and that seems to be updating as expected.

Only real difference I can note from the code as in the videos is that I am using tags to find the player, rather than the transform. I was having the same problem before doing this.

This is my enemyAI code:

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

public class EnemyAi : MonoBehaviour
    GameObject target;
    [SerializeField] float chaseRange = 10f;

    Animator enemyAnimator;
    NavMeshAgent navMeshAgent;
    float distanceToTarget = Mathf.Infinity;
    bool isProvoked = false;
     void Start()
        target = GameObject.FindGameObjectWithTag("Player");
        navMeshAgent = GetComponent<NavMeshAgent>();
        enemyAnimator = GetComponent<Animator>();

    void Update()
        distanceToTarget = Vector3.Distance(target.transform.position, transform.position);
        if (isProvoked)
        else if(distanceToTarget <= chaseRange)
            isProvoked = true;

    private void EngageTarget()
        if(distanceToTarget >= navMeshAgent.stoppingDistance)
        if(distanceToTarget <= navMeshAgent.stoppingDistance)

    private void ChaseTarget()
        enemyAnimator.SetBool("attack", false);

    private void AttackTarget()
        enemyAnimator.SetBool("attack", true);

    void OnDrawGizmosSelected()
        Gizmos.color =;
        Gizmos.DrawWireSphere(transform.position, chaseRange);

Edit – I’ve now fixed this, following something that aseyfarth said in another thread. Enabling Apply Root Motion in the enemy’s animator. This seems to override the transforms in the animation though, so I no longer have any vertical movement. It is still doing the scaling to give a squashing affect though. Kind of fun, but not exactly what I was going for.

1 Like

I spent the day experimenting with the Zombie animations from the Mixamo site and the free Zombie pack on the Asset Store. Learned some things about Root Motion and Nav Mesh Agents. If you are interested, read on:
There are a couple of relevant videos after the jump.
I discovered a bunch of bugs with the imported animations and worked over Root Motions until I realized I was starting to become bogged down. Time to move on with the lesson.

so i’m having the same issu as [X_Sponge], wich haven’t been answer. now that the enemy is animated, he wont chase me anymore. like he did, I enabled the Apply root motion, wich makes him chase me, but my idle animation doesnt work anymore. anyone know how to fix that ? thanks !

Hey so I’m way late on the reply to this but I’ve just worked it out.

Basically the issue is both the Animator and the NavMeshAgent components are both trying to affect the objects transform at the same time, so one is overriding the other. The by far simplest way to fix this is to have all the navigation controlled by a parent GameObject and all the animation and graphics controlled by a child GameObject, the only difference in the code you have to make to what is shown in the video is using GetComponentInChildren as opposed to GetComponent and just have your NavMeshAgent and EnemyAI scripts on the parent game object, and have your animator component on the child game object.

If you wanna read more there’s this: Unity - Manual: Using NavMesh Agent with Other Components in the unity documentation.

Hope this helps!

1 Like

Thanks for providing this detail; because of this thread it only took a few moments of searching for me to resolve this very issue just now.

I strongly recommend this information be added to the course in a future update – any future students who change the transform of the enemy object in their animations are going to encounter this issue. I had an idle animation that would cause the sphere to just gently bounce in place, which was causing this issue.

How can I change material color if I have few colors for one part? I want only to change color of the Eye (the blue one), but adding a property changes color for the whole part.

So I did exactly what you just said with Enabling Apply Root , now the enemy is chasing me again but he will not go over to the attack state, so now he is only following me.
What I have to do to fix this?

Is this thread related to the RPG series? If it is, then don’t worry, I think Sam addresses that bug later on, if not, then the issue has something to do with the ‘navMeshAgent’'s stopping distance, try increasing it and see if that works.

It`s from this course:

Complete C# Unity Game Developer 3D

I am lecture 6 Zombie Runner - First Person Shooter, It is Video number 167

According to the code, the distance is the one that triggers the animation, write some debug logs and see if the condition is met, if it is, then it might something with how you setup your animator.

If none of that works, try @rjcoey’s solution.

I want to try @rjcoey`s solution.
So i just create a Empty Game Object in the Hierarchy or in the animation folder and then I just put the animations in the game object?

I hope the question don`t sound too silly, I am just stuck right now.

You have to create an empty game object, reset its transform, then child the zombie to it, remove the movement scripts and navmesh from the zombie and put them into the empty game object.

The idea behind doing that, is that the animations manipulate the transform of the zombie, the navmesh does the same, they basically override each other, that would cause a lot of issues.

Hope that works.

1 Like

Thank you got it to work!

1 Like

Privacy & Terms