- Home /
Question by
NinjaEntertainment · Oct 14, 2017 at 10:24 AM ·
enemyfieldofview
Enemy can see player throught walls problem!!
Hey guys i need help with enemy field of view!! Im using vector3 distance and angles but my enemy can see my player throught walls!! i need a litle help! Here is my script!:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Enemy: MonoBehaviour {
public GameObject player;
static Animator anim;
void Start () {
anim = GetComponent<Animator> ();
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezePositionY;
}
// Update is called once per frame
void Update () {
if (player.activeInHierarchy == false) {
if (player == null) {
anim.SetBool ("isIdle", true);
anim.SetBool ("isWalking", false);
anim.SetBool ("isAttacking", false);
}
} else {
Vector3 direction = player.transform.position - this.transform.position;
direction.y = 0;
float angle = Vector3.Angle (direction, this.transform.forward);
if (Vector3.Distance (player.transform.position, this.transform.position) < 10 && angle < 90) {
this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (direction), 0.1f);
anim.SetBool ("isIdle", false);
if (direction.magnitude > 3) {
GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.FreezeRotationY;
this.transform.Translate (0, 0, 0.05f);
anim.SetBool ("isWalking", true);
anim.SetBool ("isAttacking", false);
} else {
agent.stoppingDistance = 2f;
anim.SetBool ("isAttacking", true);
anim.SetBool ("isWalking", false);
}
} else {
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezePositionY;
anim.SetBool ("isIdle", true);
anim.SetBool ("isWalking", false);
anim.SetBool ("isAttacking", false);
}
}
}
}
I don't know what to do!! Enemy's angle of view is 90 degrees and i wan't when player is hiding behind an object AND when HE IS IN DISTANCE ENEMY CAN't SEE HIM!!! PLEASE GUYS I WOULD REALLY APPRECIATE ANY ANSWER
Comment
Your answer
Follow this Question
Related Questions
If Enemy Health (< 10) then Exp (+10)? 1 Answer
Making enemy push player 1 Answer
Hurt Player if close to Enemy! Need Help 4 Answers
Make Enemy Solid Object 2 Answers
Enemy healthbar script 2 Answers