Game Development Reference
In-Depth Information
public function Shoot (_direction : Vector3) : boolean {
var hit : RaycastHit;
//Checking if the player hit the shooting range
if (Vector3.Distance(transform.position, player.position)
<= shotRange) {
// Cast ray shotRange meters in shot direction, to see
if nothing block the rocket
if (Physics.Raycast(transform.position, _direction,
hit, shotRange)) {
if (hit.transform.tag != "Wall") {
b_isAiming = true;
return b_isAiming;
}
}
}
b_isAiming = false;
return b_isAiming;
}
Second, we will create the Jump() funcion to make our enemy smarter by
using another Physics class funcion, Physics.CapsuleCast() . This
funcion will cast the capsule object from the enemy's posiion towards its
movement direcion to see if the enemy hits the wall. This will trigger the
enemy to jump over the wall and coninue walking towards its direcion as
in the following code:
//Make character Jump
public function Jump (_direction : Vector3) : boolean {
//Checking for Jumping if the next y position is different
than the current y position
var hit : RaycastHit;
var p1 : Vector3 = transform.position + controller.
center + Vector3.up * (-controller.height*0.5);
var p2 : Vector3 = p1 + Vector3.up * controller.height;
// Cast character controller shape moveSpeed meters
forward, to see if it is about to hit anything
if ((Physics.CapsuleCast (p1, p2, 0.1, _direction, hit))
&& (c_collisionFlags & CollisionFlags.Sides)) {
if (hit.transform.tag == "Wall") {
return true;
}
}
return false;
}
 
Search WWH ::




Custom Search