- Home /
Enemy AI help with height check
Hello there guys. Im making an AI and my problem is that i don't know how to check height between player and enemy.Enemy can attack player even if he is 20m above him. i don't know how to check that!! Im using Vector3 distance. Please help me if you can !! i would really appreciate it!!!!! My enemy script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EnemyRobot : MonoBehaviour {
public GameObject gun;
public Transform player;
static Animator anim;
public float attackTime;
public float coolDown = 2f;
public GameObject bullet;
public Transform spawn;
void Start () {
anim = GetComponent<Animator> ();
gun.SetActive (false);
}
// Update is called once per frame
void Update () {
Vector3 direction = player.position - this.transform.position;
float angle = Vector3.Angle (direction, this.transform.forward);
if (Vector3.Distance (player.position, this.transform.position) < 30 && angle < 180) {
direction.y = 0;
this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (direction), 0.1f);
anim.SetBool ("isIdle", false);
if (direction.magnitude > 10) {
GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY;
this.transform.Translate (0, 0, 0.05f);
anim.SetBool ("isWalking", true);
anim.SetBool ("isAttacking", false);
gun.SetActive (false);
}else {
if (attackTime > 0)
attackTime -= Time.deltaTime;
if (attackTime < 0)
attackTime = 0;
if (attackTime == 0) {
attackTime = coolDown;
GameObject ballInstance;
ballInstance = Instantiate (bullet, spawn.position, spawn.rotation) as GameObject;
ballInstance.GetComponent<Rigidbody> ().AddForce (transform.forward * 5000);
anim.SetBool ("isAttacking", true);
anim.SetBool ("isWalking", false);
gun.SetActive (true);
if (direction.magnitude < 3) {
GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.FreezeAll;
}
}
}
} else {
anim.SetBool ("isIdle", true);
anim.SetBool ("isWalking", false);
anim.SetBool ("isAttacking", false);
gun.SetActive (false);
}
}
}
Answer by NinjaEntertainment · Oct 07, 2017 at 08:33 PM
GUYS I MADE IT!! HERE IS THE SCRIPT NOW: using UnityEngine; using System.Collections; using System;
public class EnemyRobot : MonoBehaviour {
public GameObject gun;
public Transform player;
static Animator anim;
public float attackTime;
public float coolDown = 2f;
public GameObject bullet;
public Transform spawn;
void Start () {
anim = GetComponent<Animator> ();
gun.SetActive (false);
}
// Update is called once per frame
void Update () {
if (Mathf.Abs (this.transform.position.y - player.transform.position.y) < 2f) { // He is above or below}
Vector3 direction = player.position - this.transform.position;
float angle = Vector3.Angle (direction, this.transform.forward);
if (Vector3.Distance (player.position, this.transform.position) < 30 && angle < 180) {
direction.y = 0;
this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (direction), 0.1f);
anim.SetBool ("isIdle", false);
if (direction.magnitude > 10) {
GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY;
this.transform.Translate (0, 0, 0.05f);
anim.SetBool ("isWalking", true);
anim.SetBool ("isAttacking", false);
gun.SetActive (false);
} else {
if (attackTime > 0)
attackTime -= Time.deltaTime;
if (attackTime < 0)
attackTime = 0;
if (attackTime == 0) {
attackTime = coolDown;
GameObject ballInstance;
ballInstance = Instantiate (bullet, spawn.position, spawn.rotation) as GameObject;
ballInstance.GetComponent<Rigidbody> ().AddForce (transform.forward * 5000);
anim.SetBool ("isAttacking", true);
anim.SetBool ("isWalking", false);
gun.SetActive (true);
if (direction.magnitude < 10) {
GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.FreezeAll;
}
}
}
} else {
GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.FreezeAll;
anim.SetBool ("isIdle", true);
anim.SetBool ("isWalking", false);
anim.SetBool ("isAttacking", false);
gun.SetActive (false);
}
}
}
}
Im checking height before distance... :DDD ALL THANKS TO fafase
Your answer
Follow this Question
Related Questions
Check if object is moving towards a point 1 Answer
Check distance between many objects 3 Answers
Make Navmesh agent follow closest object with tag 0 Answers
How do you find the distance of multiple objects in a array 1 Answer
Oblique launch problem 0 Answers