Enemy rotating on Y for some unknown reason

Hello, I posted in Unity Answers but the guys over there don’t really help out so well. I’m having a problem with my enemy when I get close to him he will notice I am there and when I get closer he attacks as says in debug log. The problem I’m having is the enemy will rotate on the Y I’m guessing so say he starts at y-0 when I get closer to him he will rotate to about y-40 for some reason then my walk animation kicks in and he starts walking towards under the terrain and not towards my player I have an image which I will attach if you need to see my coding I can post it too. Someone said on the Answers that it is because my lookAt() function has a transform.rotation in it that is doing something to it which makes it do what it does but If I take off the rotation Q…slerp script my enemy doesn’t see my player and when I get close he will walk towards the end of my map and not towards my player. I’m ideally just trying to make the enemy see my player, move around still on his own maybe following way points and then when my player is close enough it attacks me.

He see’s me, he looks for me within a distance if I go too far away he will stand in his idle pose until i get closer and the closer I get he rotates on Y I frooze rotation on the ridgidbody but doesn’t work, I did think it was my model mesh but seems it isn’t it’s the actual model that rotates.

 var Distance;
var Target : Transform;
var lookAtDistance = 25.0;
var attackRange  = 15.0;
var moveSpeed = 5.0;
var Daping = 6.0;
var myTrans : Transform;
private var anim : Animator;
function Start () {
         anim = GetComponent(Animator);
}
function Update ()
{
             Distance = Vector3.Distance(Target.position, transform.position);
             if (Distance < lookAtDistance)
             {
                 anim.SetBool("Walking", false);
                 Debug.Log("Looking at Player!");
                 lookat ();
                
             }
            
             if (Distance > lookAtDistance)
             {   
                 Debug.Log("Looking For Player!");
                
             }
            
             if (Distance < attackRange)
            
             {
                 transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
                 //anim.SetBool("Walking", true);
                 Debug.Log("Attacking");
             }
            
            
}
function lookat () {
     var rotation = Quaternion.LookRotation(Target.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Daping);
}
function attackPlayer () {
     //transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}

If anyone can help me out that would be awesome has I have attack animations set up already and everything else I just can’t get my AI to do what I want it to do. Please note this is not a pokemon game at all I’m using the models as temp models as my player still needs rigging and these models I’m using are rigged and not with miximo autorigger as I dislike working with that rigger ALOT.

Also take into consideration once I’ve sorted the problem and it works I will post my code for anyone else having the same problem as once it works the code will be adapted to my needs so I can post what and where I went wrong for future people looking for the same type of help :slight_smile:

is it looking at the players feet?.. i.e. the player position is the feet but the enemy position is somewhere in the middle?

check the z position of the enemy and player position, if they’re not the same it’ll “angle down”.

I think it might be you know :confused: like the pivot for my player is at the bottom and the enemys is middle of his chest, will it be just changing the pivot? can you give me an example of how I might be able to fix this as I’ve been trying new ways of sorting it out and nothing has come yet, I even made just a look script for looking at my player and it does the same so it’s not my script above {its the lookat()} function thats shit :frowning:

Just checked and the AI’s Z is going 0.012 and above then below. my Players Z is 0 all the time sooo not sure what to do as I’ve never come across this before :confused: