Animation to my zombie

OK guys i have bin n this all day looking for post to try and help me but i cant find any.

What i need is for someone to tell me how to give my zombie animations i downloaded the zombie and animations from the asset store the animations work and zombie is fine but i can make him do the animations i want when he is chasing me and when he is attacking me.

I will explain my AI first what it does is detects me by sight or by hearing me then starts to chase me simple. I have my starting animation set and the delay so he doesn’t chase me until the animation runs through as my first animation is him getting up off the floor.

My problem is when he chases me i cant seem to get him to do the walking animation it just freezes after the first animation runs through and he is like a icecube chasing me totally still but still moving.

I have had a look at alot of post and cant seem to get to grips wit the answer as bear in mind i am new to this and still a newbie so when you answer please be simple and give me a straight answer none of the technical talk :stuck_out_tongue: i will post my script below so you can edit it and maybe post it back to me that would be great.

also i have a separate script for the enemy attack and i need to know how to make him do the attack animation when he actually damages me.

I will post both scripts and if you are kind enough to edit the script on the animation name slots just say (“walk”) and (“attack”)

thanks a bunch.

var target : Transform;
var enemySightDistance = 20.0;
var enemyHearingDistance = 10.0;
var rotationSpeed = 3.0;
var moveSpeed = 4.0;
var maxAttackDistance = 0.0;
private var myPosition : Transform;

function Awake()
{
	myPosition = transform;
}

function Start()
{
	var go = GameObject.FindGameObjectWithTag("Player");
	
	target = go.transform;
	
	maxAttackDistance = 2.0;
}

function Update()
{
	WatchForPlayer();
	ListenForPlayer();
}

function WatchForPlayer()
{
	var dir : Vector3 = (target.transform.position - transform.position).normalized;
	var direction = Vector3.Dot(dir, transform.forward);
	
	if(Vector3.Distance(target.position, myPosition.position) < enemySightDistance && direction > 0)
	{
		PlayerDetected();
		animation.Play("get_up");
	}
}

function ListenForPlayer()
{
	if(Vector3.Distance(target.position, myPosition.position) < enemyHearingDistance)
	{
		PlayerDetected();
		animation.Play("get_up");
	}
}

function PlayerDetected()
{
	yield WaitForSeconds(2.5);
	Debug.DrawLine(target.position, myPosition.position, Color.red);
	
	myPosition.rotation = Quaternion.Slerp(myPosition.rotation, Quaternion.LookRotation(target.position - myPosition.position), rotationSpeed * Time.deltaTime);
	if(Vector3.Distance(target.position, myPosition.position) > maxAttackDistance)
	{
		animation.Play("injured_walk");
		myPosition.position += myPosition.forward * moveSpeed * Time.deltaTime;
	}
}

that is my AI script ^

And here is my attack script

var target : GameObject;
var attackTimer : float;
var coolDown : float;

function Start()
{
	attackTimer = 0.0;
	coolDown = 1.5;
}

function Update()
{
	if(attackTimer > 0)
		attackTimer -= Time.deltaTime;
	if(attackTimer < 0)
		attackTimer = 0;
		
	
	if(attackTimer == 0)
	{
		Attack();
		attackTimer = coolDown;
	}

}

function Attack()
{
	var distance = Vector3.Distance(target.transform.position, transform.position);
	var dir : Vector3 = (target.transform.position - transform.position).normalized;
	var direction = Vector3.Dot(dir, transform.forward);
	
	Debug.Log(direction);
	
	if(distance <= 2.5 && direction > 0.3)
	{
		var ph : PlayerHealth = target.GetComponent("PlayerHealth");
		ph.AdjustCurrentHealth(-10);
	}
}

You are calling WatchForPlayer and ListenForPlayer every update,
so it keeps checking if in range and playing “get_up” ,
that is why the character is ‘frozen’ (it keeps playing “get_up” from the beginning every frame).
You need some boolean to check if the player has already been detected or not.
But then you need to organze a better way to wait for the enemy to get up and start walking without continuing to check for player.
Calling a function every frame with a yield in it is not a good idea.

I can see many other problems in the future for your script, but for now I shall try and get what you have working.

Here is an edit that includes all the considerations I have mentioned so far :

var target : Transform;
var enemySightDistance = 20.0;
var enemyHearingDistance = 10.0;
var rotationSpeed = 3.0;
var moveSpeed = 4.0;
var maxAttackDistance = 0.0;
private var myPosition : Transform;

private var isPlayerDetected : boolean = false;
private var isEnemyStanding : boolean = false;

function Awake()
{
	myPosition = transform;
}

function Start()
{
	var go = GameObject.FindGameObjectWithTag("Player");
	
	target = go.transform;
	
	maxAttackDistance = 2.0;
}

function Update()
{
    if ( !isPlayerDetected ) // means if ( isPlayerDetected == false )
    {
        WatchForPlayer();
        ListenForPlayer();
    }
	else if ( isEnemyStanding ) // means if ( isEnemyStanding == true )
	{
		FollowPlayer();
	}
}

function WatchForPlayer()
{
	var dir : Vector3 = (target.transform.position - transform.position).normalized;
	var direction = Vector3.Dot(dir, transform.forward);
	
	if(Vector3.Distance(target.position, myPosition.position) < enemySightDistance && direction > 0)
	{
		PlayerDetected();
		animation.Play("get_up");
	}
}

function ListenForPlayer()
{
	if(Vector3.Distance(target.position, myPosition.position) < enemyHearingDistance)
	{
		PlayerDetected();
		animation.Play("get_up");
	}
}

function PlayerDetected()
{
	isPlayerDetected = true;
	
	yield WaitForSeconds(2.5);
	
	isEnemyStanding = true;	
	animation.Play("injured_walk");
}

function FollowPlayer()
{
	Debug.DrawLine(target.position, myPosition.position, Color.red);
	
	myPosition.rotation = Quaternion.Slerp(myPosition.rotation, Quaternion.LookRotation(target.position - myPosition.position), rotationSpeed * Time.deltaTime);
	
	if (Vector3.Distance(target.position, myPosition.position) > maxAttackDistance)
	{
		myPosition.position += myPosition.forward * moveSpeed * Time.deltaTime;
	}
}

With that, once the player is detected ( PlayerDetected is called ),
isPlayerDetected becomes true, so every update after that,
the detecting functions in update are not called.
This should leave your enemy to stand up, set isEnemyStanding to true, then play “injured_walk”.
In update when isEnemyStanding is true, only the movement function is called (without any animation.Play overriding current animation.