How do I stop my enemy from falling over?

Hey there, I got this simple game in the process of being made, and so far I’m trying to create an enemy AI that follows the player and attacks them when you get to close. I got my enemy to follow the player, and for now I’m fixing any issues that occur once he gets close, and one of them involve my enemy falling over.

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

public class Enemy : MonoBehaviour

private GameObject myTarget;
private Transform targetPos;
public float speed;

void Start () 

    myTarget = GameObject.FindGameObjectWithTag("Player");
    targetPos = myTarget.transform;

void Update () 
        transform.position = Vector3.MoveTowards(transform.position, targetPos.position, speed * Time.deltaTime);

     Vector3 lookVector = myTarget.transform.position - transform.position;
     lookVector.y = transform.position.y;
     Quaternion rot = Quaternion.LookRotation(lookVector);
     transform.rotation = Quaternion.Slerp(transform.rotation, rot, 1);



This is top to bottom of the code I got so far. I do have a Rigidbody attached to my enemy, and is most likely the reason their falling over; but they still fall over, but begin to float as well.

There are many ways to properly rotate a transform or rigidbody on y axis only. But to not rewrite too much of the existing code, you can just change

lookVector.y = transform.position.y;


lookVector.y = 0;

Why? Because the y component here represents the height difference between enemy position point and a point at the direction line that points to the player. So, the closer the enemy gets to the player, the more significant the y component becomes in the resulting direction vector. To eliminate this, you need to set the direction horizontal factor (the y) to 0, so the resulting look direction will be completely independent of the heights, and only the x (left-right difference) and z (forward-backwards difference) will be counted.

PS: when you tried to freeze the rotation by setting rigidbody constraints, you have constrained the rigidbody properties only. The thing is, rigidbody and transform are 2 different components on the game object. And the rigidbody’s rotation constraints only affect the rigidbody. If you were manipulating the enemy with rigidbody special functions like AddForce() to push the enemy with some physical impulse force or AddTorque() to spin the rigidbody, the constraints would be applied to the resulting motion internally by the physics engine. But since you manipulate the transform directly, the rigidbody constraints are not counted at all.

Do you have a colliders for enemies either?
Have you checked your forward for enemies? it’s Z axis arrow.
Have you tried to set only y rotations for enemies? Now you are trying to rotate every axis.

maybe in the inspector freeze rotation in the rigidbody