Game Development Reference
In-Depth Information
5. After adding the Start() funcion, we need the funcion to check for the
CollisionFlags parameter of the enemy to see if our enemy hits the ground,
which is very similar to the CharacterControl script that we used in Project 4 ,
The Hero/Heroine Part II - Animaion and Controls :
//Checking if the character hit the ground (collide Below)
public function IsGrounded () : boolean {
return (c_collisionFlags & CollisionFlags.CollidedBelow);
}
6. Next, create OnCollisionEnter() , which is the built-in funcion to check whether
the enemy got hit by the player, and decrease the hit-points if necessary. Also, we
can add another funcion to get the percent of the current enemy hit-points and
maximum enemy hit-points by adding the following code:
//Checking for the collision if the rocket hit the enemy
public function OnCollisionEnter(collision : Collision) : void {
if (StaticVars.b_isGameOver == false) {
if (collision.transform.tag == "Rocket") {
var rocket : Rocket = collision.gameObject.
GetComponent(Rocket);
var f_damage : float = rocket.getDamage();
//Clamp down the hitpoint - not lower than 0, and not higher
than max hitpoint
aiHP = Mathf.Clamp(aiHP-f_damage, 0, aiMaxHP);
}
}
}
//Get the percent of the maximum HP with the current HP
public function GetHpPercent() : float {
return aiHP/aiMaxHP;
}
7. Then, we will create four funcions to give the enemy a personality and make our
enemy smarter:
The first one is the Shoot() funcion, which will make the enemy shoot
when the player is within shooing range of the enemy by checking the
distance of the player and enemy. We will also use the Physics.Raycast()
funcion to see if there is any wall blocking the direcion of the shot; if there
isn't, we just make the enemy shoot by adding the following code:
//Give the Enemy Characteristic
///////////////////////////////////////////////////////////
///
//Checking for the character is shooting
 
Search WWH ::




Custom Search