Enemy AI HELP PLEASE

Hello guys i made a enemy with enemy script and animator(with bools). It’s working perfect but i have 2 problems. He can attack a player of different height. Here is the screenshot of the “problem”

And as you can see that enemy is attacking from different height. And 2nd problem is that he is floating. he have rigidbody how can i stop him from floating…
Also here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyChase : MonoBehaviour {
	
	public Transform player;
	static Animator anim;

	void Start () {
		anim = GetComponent<Animator> ();
	}
	
	// Update is called once per frame
	void Update () {
		if (Vector3.Distance (player.position, this.transform.position) < 10) {
			
			Vector3 direction = player.position - this.transform.position;
			direction.y = 0;

			this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (direction), 0.1f);
			anim.SetBool ("isIdle", false);
			if (direction.magnitude > 3) {
				this.transform.Translate (0, 0, 0.05f);
				anim.SetBool ("isWalking", true);
				anim.SetBool ("isAttacking", false);
			} else {
				anim.SetBool ("isAttacking", true);
				anim.SetBool ("isWalking", false);
			}
			

		} else {
			anim.SetBool ("isIdle", true);
			anim.SetBool ("isWalking", false);
			anim.SetBool ("isAttacking", false);

		}
	}
}

He can shoot when you are above because you are not checking about it. You are actually discarding the y value. So even though you are 10 or 200m above, your enemy “sees” your player just next and will aim at him.

You would have to add a altitude check as well as a vision check.

Use a linecast between the enemy and the player so it can check whether it is seeing it or not.

Then if it can see it, check the y values.

if(Mathf.Abs(enemy.transform.y - player.transform.y) > 2f){ // He is above or below}

So as pseudo code you get:

void Udpate()
{
    if (distance > maxDistance) return;
    if(LinecastSuccess == false) return;
    if(AltitudeCheck > maxAltitudeDifference) return;
    Shoot();
}

The flying problem is because you are moving the enemy with transform. When a NPC has Rigidbody or CharacterController, if you use transform, that won’t do.
Try the following to begin with:

Hi… that check didn’t even worked… my enemy still can hit me from 10 meters above. :frowning:
Can you please fill the script?? this don’t work :frowning: please man…
he just keeps playing attack animation when i come near… :((