Не получается сделать прыжок врага в 2D Платформере на Unity
Как я понял, проблема в том, что враг преследует игрока по осям x и у (y стоит на 0) и из-за этого враг не прыгает когда достигает возвышения, но не знаю как решить. вот скрипт:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public int health = 20;
public int damage = 10;
Animator anim;
public Transform player;
Rigidbody2D rb;
public float speed;
public float agroDistance;
public float jumpForce;
public void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
public void TakeDamage(int damage)
{
health -= damage;
if (health <= 0)
{
Die();
}
}
public void Die()
{
Destroy(gameObject);
}
private void Update()
{
float distToPlayer = Vector2.Distance(transform.position, player.position);
if (distToPlayer < agroDistance)
{
StartHunting();
}
else
{
StopHunting();
}
}
void StartHunting()
{
anim.SetBool("isWalking", true);
if (player.position.x < transform.position.x)
{
rb.velocity = new Vector2(-speed, 0);
transform.localScale = new Vector2(3, 3);
}
else if (player.position.x > transform.position.x)
{
rb.velocity = new Vector2(speed, 0);
transform.localScale = new Vector2(-3, 3);
}
}
void StopHunting()
{
anim.SetBool("isWalking", false);
rb.velocity = new Vector2(0, 0);
}
void Jump()
{
Debug.Log("adasd");
rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Ground"))
{
Jump();
}
}
}