Zombie Attack, Health Reduction, and Zombie Chase
- Keshav Batra
- Jun 3, 2021
- 1 min read
So what I did here was I just programmed the animation to move the zombie and to have it attack me. Here is the code for the attacking and the chasing:
void Patrolling()
{
if (!walkPointSet) SearchWalkPoint();
//if (walkPointSet) SearchWalkPoint();
if (walkPointSet)
{
agent.SetDestination(walkPoint);
AnimationController.SetBool("IsRunning", true);
AttackAnimation.SetBool("IsAttacking", false);
}
Vector3 distancetoWalkPoint = transform.position - walkPoint;
//Walkpoint reached
if (distancetoWalkPoint.magnitude < 1f)
walkPointSet = false;
}
Chasing
private void ChasePlayer()
{
agent.SetDestination(player.position);
AnimationController.SetBool("IsRunning", true);
AttackAnimation.SetBool("IsAttacking", false);
}
Attacking
private void AttackPlayer()
{
agent.SetDestination(transform.position);
Vector3 LookPosition = player.position;
LookPosition.y = transform.position.y;
transform.LookAt(LookPosition);
}
Now here's the code for the health deduction:
if (!alreadyAttacked)
{
player.gameObject.GetComponent<PlayerMovement>().TakeDamage(10); //the first thing it needs is a reference to another script
alreadyAttacked = true;
Invoke(nameof(ResetAttack), timeBetweenAttacks);
AnimationController.SetBool("IsRunning", false);
AttackAnimation.SetBool("IsAttacking", true);
}
Now here's the end result:
Now it's looking like an actual enemy movement.
Comments