@DuckU

Как сделать чтобы враг смотрел в сторону игрока?

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

public class Enemy : MonoBehaviour
{

    public float speed;
    public int HP;
    public GameObject effect;

    public static int ColEnemy;

    private Transform target;

    void Start()
    {
        ColEnemy = Spawn.MaxSpawnEnemy;
        target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
    }

    void Update()
    {
        if (HP <= 0)
        {
            Instantiate(effect, transform.position, Quaternion.identity);
            ColEnemy--;
            Destroy(gameObject);
        }
        transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            {
                Player.HP = Player.HP - 50;
                ColEnemy--;
                Instantiate(effect, transform.position, Quaternion.identity);
                Destroy(gameObject);
            }
        }
    }

    public void TakeDamage(int damage)
    {
        HP -= damage;
    }
}

Это скрипт врага.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Player : MonoBehaviour
{

    public static float speed = 3; // Скорость игрока
    private Vector2 direction;
    private Rigidbody2D rb;
    public float Ofset; // + к повороту игрока

    public GameObject PanelRespaun;

    public static float HP; // Здоровье игрока
    public static float MaxHP = 100; // Максимальное здоровье игрока
    public static float Eat;

    void Start()
    {
        PanelRespaun.SetActive(false);
        rb = GetComponent<Rigidbody2D>();
        HP = MaxHP; // Начальное здоровье
        Eat = 100f;
    }
   
    void Update()
    {
        Debug.Log(HP);
        direction.x = Input.GetAxisRaw("Horizontal"); // Движение игрока
        direction.y = Input.GetAxisRaw("Vertical"); 

        if (HP <= 0) // Событие при смерьти игрока
        {
            PanelRespaun.SetActive(true);
            MaxHP = 100;
            HP = 100;
            speed = 3;
            S.Wood = 0;
            S.Ston = 0;
            Destroy(gameObject);
        }

        Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position ; // Поворот игрока за мышкой
        float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg ;
        transform.rotation = Quaternion.Euler(0f, 0f, rotZ + Ofset);
    }

    void FixedUpdate()
    {
        rb.MovePosition(rb.position + direction * speed * Time.fixedDeltaTime); // Само движение
    }

}

Это скрипт игрока.
У меня есть 2д проект с видом с вверху, и надо чтобы враг смотрел в сторону игрока когда идёт. Желательно по ратейшену
  • Вопрос задан
  • 336 просмотров
Пригласить эксперта
Ответы на вопрос 1
@Gosha0110
Можно создать 4 позиции которые будут в верху, в низу, с лева и с права и смотря к какой точке игрок будет ближе туда и поворачиваться.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы