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.

1 Like

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)
        {
            EngageTarget();
        }
        else if(distanceToTarget <= chaseRange)
        {
            isProvoked = true;
        }
    }

    private void EngageTarget()
    {
        if(distanceToTarget >= navMeshAgent.stoppingDistance)
        {
            ChaseTarget(); 
        }
        
        if(distanceToTarget <= navMeshAgent.stoppingDistance)
        {
            AttackTarget();
        }
    }

    private void ChaseTarget()
    {
        enemyAnimator.SetBool("attack", false);
        enemyAnimator.SetTrigger("move");
        navMeshAgent.SetDestination(target.transform.position);
        print(target.transform.position);
    }

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

    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        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.

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:

http://devlog.merlinsbeard.net/2020/10/23/animation-adventures/
There are a couple of relevant videos after the jump.

http://devlog.merlinsbeard.net/2020/10/23/animator-woes/
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 !