The enemy walks up in the air (238984)

So, my problem is that when the enemy chases the main character. When my character jumps on the high ground or just jumps, the enemy starts walking in the air. So here’s my code: I would like to know what else I can add to my code.

public class EnemyAl : MonoBehaviour
{


    public Transform Target;
    public float MoveSpeed;
    public float AttackAnimDuration = 3f;
    
    private bool paused = false;
    public float speed = 3f;

    private float attackTimer =0f;



    // Start is called before the first frame update
    void Start()
    {
        
    }
    
    // Update is called once per frame
    void Update()
    {
       

        if (!paused)
        {
            float move = MoveSpeed * Time.deltaTime;
            transform.position = Vector3.MoveTowards(transform.position, Target.position, move);
        }
        else
        {
            attackTimer += Time.deltaTime;
            if(attackTimer >= AttackAnimDuration)
            {
                attackTimer = 0;
                paused = false;
            }
        }
    }
    void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            paused = true;
        }
    }
}

1 Answer

1

I imagine Transform Target is the transform of the player. When you use MoveTowards(), you are passing the exact player position as the end position. So, if your player jumps or goes up or down in anyway, the enemy will just MoveTowards that position. You could just keep the code the same and just add a ground check with a raycast. If there is a collider on the ground, you can Raycast directly down from the enemy’s new position to find the position on the ground (you may want to add a unit to the y axis in case the new position is in a collider below already).