Как заставить один объект преследовать другой исключительно по горизонтали в Unity2D?

Мне нужно сделать так, чтобы "враг" преследовал игрока. При этом, чтобы у врага не резко прерывалась анимация, то есть, он начинал преследовать с той же позиции, с которой закончилась анимация. И ещё, нужно чтобы "враг" двигался только по горизонтали. Я пытался с помощью этого кода, но враг, естественно, начинает преследовать по вертикали:

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

public class Enemy : MonoBehaviour
{
    public float checkRadius;
    private bool isPlayerInRange;
    public Transform playerCheck;
    public LayerMask groupOfPlayer;
    private Transform playerPosition;
    public GameObject player;
    public float speed;
    private Animator anim;
    public float whatTime;
    public GameObject cam;
    // Start is called before the first frame update
    void Start()
    {
        playerPosition = player.GetComponent<Transform>();

    }

    // Update is called once per frame
    void Update()
    {
        Animator anim = GetComponent<Animator>();
        isPlayerInRange = Physics2D.OverlapCircle(transform.position, checkRadius, groupOfPlayer);
        if (isPlayerInRange && Input.GetKey(KeyCode.LeftShift) == false)
        {
            anim.enabled = false;
            Invoke("chaseAfterPlayer", whatTime);
        }
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            Destroy(player);
            cam.GetComponent<CameraFollowPlayer>().enabled = false;
            Invoke("restart", 2f);
        }
    }
    void chaseAfterPlayer()
    {
        transform.position = Vector2.MoveTowards(transform.position, playerPosition.position, speed * Time.deltaTime);
    }
    void restart()
    {
        SceneManager.LoadScene("SampleScene");
    }
}

Ответы (0 шт):