hi, i am making a zombie game and bascially i want to make the zombie attack the player but not sure how to code it?
my zombie animation has an axe which it hits with but not sure how i would go about implementing damage to the player. if you could please help…thanks
this is my code so far.
// Public Variables
// the enemy's speed
public var speed:float = 3;
// the distance within which the enemy will detect a player, from the waypoint
public var playerDetectionRadius:int = 10;
// the max distance the enemy will chase the player to, from the waypoint
public var maxWaypointDistance:int = 20;
// the player that the enemy will follow
public var player:GameObject;
// the waypoint object that the enemy is commanding / guarding
public var waypoint:GameObject;
// Private variables
// whether the enemy is chasing the player
private var chase:boolean;
// whether the enemy is waiting for the player
private var waiting:boolean;
private var attackRange = 3.5;
private var dontComeCloserRange = 2;
function Update () {
// the current velocity of the rigidbody component
var velocity:Vector3 = rigidbody.velocity;
// difference between the enemy and the waypoint
var waypointDist:Vector3 = waypoint.transform.position - transform.position;
waypointDist.y = 0;
// difference between the enemy and the player
var playerPosition:Vector3 = player.transform.position - transform.position;
playerPosition.y = 0;
// difference between the player and the waypoint
var playerDist:Vector3 = player.transform.position - waypoint.transform.position;
playerDist.y = 0;
// if the distance from the player to the waypoint is less than the playerDetectionRadius
if (playerDist.magnitude < playerDetectionRadius)
{
chase = true;
waiting = false;
}
// if the distance from the enemy to the waypoint is greater than the maximum allowed, stop chasing
if (waypointDist.magnitude > maxWaypointDistance) chase = false;
// if we are chasing the player
if (chase)
{
// set the velocity of the enemy to the direction of the player with the specified speed
velocity = playerPosition.normalized * speed;
// play the walking animation
animation.CrossFade("Walk_Axe");
// if enemy is within attack range than attack
if(playerPosition.magnitude < attackRange)
animation.CrossFade("Attack_1");
}
else
{
// if the enemy is not waiting
if (!waiting)
{
// if distance to waypoint is really small
if (waypointDist.magnitude <= 0.5)
{
// set velocity to zero, play stand animation and set waiting to true
velocity = Vector3.zero;
waiting = true;
animation.CrossFade("Attack_Stance");
}
else
{
// set the velocity of the enemy to the direction of the waypoint with the specified speed
velocity = waypointDist.normalized * speed;
// play the walking animation
animation.CrossFade("walk");
}
}
}
// update the velocity
rigidbody.velocity = velocity;
// lookat the player
if (chase) transform.LookAt(player.transform);
else transform.LookAt(waypoint.transform);
// reset y position and rotation to 0 to remain on floor
transform.position.y = 0;
transform.eulerAngles.x = 0;
}