Hi all, i have managed to merge tow scripts now enemy in game can see player and react within his FOV
But problem is that the enemy continue shooting and hurting the player even through a wall… here is the script can some one help me
var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var attackRange = 13; // distance within which to attack
var chaseRange = 15; // distance within which to start chasing
var giveUpRange = 18; // distance beyond which AI gives up
var attackRepeatTime : float = 0.5; // delay between attacks when within range
var anim : GameObject;
var maximumHitPoints = 5.0;
var hitPoints = 5.0;
var attack : AudioClip;
private var chasing = false;
private var attackTime : float;
var checkRay : boolean = false;
var idleAnim : String = "idle";
var walkAnim : String = "walk";
var attackAnim : String = "attack";
var wanderAnim: String = "wander";
var dontComeCloserRange : int = 4;
var ZmaximumHitPoints = 100.0;
var ZhitPoints = 100.0;
var deadReplacement : Rigidbody;
var GOPos : GameObject;
private var scoreManager : ScoreManager;
var playerObject : GameObject; // the player
var fieldOfViewRange : float; // in degrees (I use 68, this gives the enemy a vision of 136 degrees)
var minPlayerDetectDistance : float; // the distance the player can come behind the enemy without being deteacted
var rayRange : float; // distance the enemy can "see" in front of him
private var rayDirection = Vector3.zero;
private var myTransform : Transform; //current transform data of this enemy
function Awake(){
myTransform = transform; //cache transform data for easy access/preformance
anim.animation.wrapMode = WrapMode.Loop;
anim.animation[attackAnim].wrapMode = WrapMode.Once;
anim.animation[attackAnim].layer = 2;
anim.animation.Stop();
}
function Start(){
target = GameObject.FindWithTag("Player").transform;
var GO = gameObject.FindWithTag("ScoreManager");
scoreManager = GO.GetComponent("ScoreManager");
}
function ApplyDamage (damage : float) {
if (ZhitPoints <= 0.0)
return;
// Apply damage
ZhitPoints -= damage;
scoreManager.DrawCrosshair();
// Are we dead?
if (ZhitPoints <= 0.0)
Replace();
}
function Replace() {
// If we have a dead barrel then replace ourselves with it!
if (deadReplacement) {
var dead : Rigidbody = Instantiate(deadReplacement, GOPos.transform.position, GOPos.transform.rotation);
scoreManager.addScore(20);
// For better effect we assign the same velocity to the exploded barrel
dead.rigidbody.velocity = rigidbody.velocity;
dead.angularVelocity = rigidbody.angularVelocity;
}
// Destroy ourselves
Destroy(gameObject);
}
function Update () {
// check distance to target every frame:
var distance = (target.position - myTransform.position).magnitude;
if (distance < dontComeCloserRange)
{
moveSpeed = 0;
anim.animation[idleAnim].speed = 0.2;
anim.animation.CrossFade(idleAnim);
}else
{
moveSpeed = Random.Range(4, 6);
anim.animation.CrossFade(walkAnim);
}
if (chasing || ZhitPoints < 100)
{
GetComponent("WanderBehavior").enabled = !GetComponent("WanderBehavior").enabled;
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
// give up, if too far away from target:
if (distance > giveUpRange)
{
chasing = false;
GetComponent("WanderBehavior").enabled = true;
}
// attack, if close enough, and if time is OK:
if (distance < attackRange Time.time > attackTime)
{
var hit : RaycastHit;
rayDirection = playerObject.transform.position - transform.position;
if((Vector3.Angle(rayDirection, transform.forward)) < fieldOfViewRange)
{ // Detect if player is within the field of view
if (Physics.Raycast (transform.position, rayDirection, hit, rayRange))
{
if (hit.transform.tag == "Player")
{
target.SendMessage( "PlayerDamage", maximumHitPoints);
}
else
{
//Debug.Log("Can not see player");
chasing = false;
}
}
}
anim.animation[attackAnim].speed = 1.2;
//anim.animation.CrossFade(attackAnim);
anim.animation.Play(attackAnim);
attackTime = Time.time + attackRepeatTime;
audio.PlayOneShot(attack, 0.5 / audio.volume);
}
}
else
{
// not currently chasing.
anim.animation[wanderAnim].speed = 1.2;
anim.animation.Play(wanderAnim);
// start chasing if target comes close enough
if (CanSeeTarget())//(distance < chaseRange)
{
chasing = true;
}
else
chasing = false;
}
}
function OnDrawGizmosSelected ()
{
// Draws a line in front of the player and one behind this is used to visually illustrate the detection ranges in front and behind the enemy
Gizmos.color = Color.magenta; // the color used to detect the player in front
Gizmos.DrawRay (transform.position, transform.forward * rayRange);
Gizmos.color = Color.yellow; // the color used to detect the player from behind
Gizmos.DrawRay (transform.position, transform.forward * -minPlayerDetectDistance);
}
function CanSeeTarget () : boolean
{
var hit : RaycastHit;
rayDirection = playerObject.transform.position - transform.position;
var distanceToPlayer = Vector3.Distance(transform.position, playerObject.transform.position);
if(Physics.Raycast (transform.position, rayDirection, hit))
{ // If the player is very close behind the enemy and not in view the enemy will detect the player
if((hit.transform.tag == "Player") (distanceToPlayer <= minPlayerDetectDistance)){
//Debug.Log("Caught player sneaking up behind!");
return true;
}
}
if((Vector3.Angle(rayDirection, transform.forward)) < fieldOfViewRange)
{ // Detect if player is within the field of view
if (Physics.Raycast (transform.position, rayDirection, hit, rayRange))
{
if (hit.transform.tag == "Player") {
//Debug.Log("Can see player");
return true;
}
else
{
//Debug.Log("Can not see player");
return false;
}
}
}
}