Enemy model , AI and animation is not working properly

So hi guys , i am just learning unity and and is running into a bunch and by that i mean a bunch of problems. First of all my enemy character completely loses its shape when i make a humanoid rig out of it so when i use lookat function with generic rig , his whole body turns up and down . He even faces the opposite way when using lookat but i did some things and fixed (if u guys have a better solution i can do in either blender or unity to fix it pls tell). Secondly and most important one its animation . I have made a script where it should chase player when in a certain range( 10f ) and thus play the run animation . But i also want it to stop running behind the player when the player is beyond limits and thus change to idle animation . But the distance between enemy and player is stuck at 9.91 or something after 9.5f and the enemy keeps on playing run animation . Please tell me a fix for it . My game is in 3D .

Enemy Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class EnemyCOntroller : MonoBehaviour
{
    public float lookRadius = 15f;
    NavMeshAgent agent;
    Transform target;
    float distance;
    private Animator animator;
    private float horizontalInput;
    private float attackRAnge = 7f;

    // Start is called before the first frame update
    void Start()
    {
        target = PlayerManager.instance.player.transform;
        agent = GetComponent<NavMeshAgent>();
        animator = GetComponent<Animator>();
       
    }

    // Update is called once per frame
    void Update()
    {
        horizontalInput = Input.GetAxis("Horizontal");
        distance = Vector3.Distance(target.position, transform.position);
        attackRAnge = Vector3.Distance(target.position, transform.position);
        if (distance <= lookRadius)
        {
            agent.SetDestination(target.position);
            animator.SetFloat("Run", attackRAnge) ;
            
            FaceTarget();
        }
        else
        {
            
        }
        
        if (distance <= 7f)
        {

            agent.SetDestination(transform.position);

        }
        

        animator.SetFloat("Attack", attackRAnge);
    }
    
   


    void FaceTarget()
    {

        transform.LookAt(2 * transform.position - target.position);


    }


    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, lookRadius);
    }

    


}

![

alt text

][1]

Split your questions into separate questions so it’s easier for us to understand.

  1. With the first question, can you show a video of exactly what happens when the model goes crazy. I think a part of the LookAt issue is you’re doing it on the whole model, not just the head - but if this is correct, I think you need to Google how LookAt works
  2. Go through your code step by step, make debug.log() bits so you see exactly what’s happening when running away

Can somebody help?