Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class EnemyFollow : MonoBehaviour
- {
- public Transform player; // Ссылка на трансформ игрока
- public float speed = 5f; // Скорость врага
- public int health = 100; // Здоровье врага
- public int damage = 10; // Урон, наносимый игроку
- private void Update()
- {
- // Направление от врага к игроку
- Vector3 direction = (player.position - transform.position).normalized;
- // Движение врага
- transform.position += direction * speed * Time.deltaTime;
- // Поворот врага влево или вправо
- if (direction.x > 0)
- {
- // Поворот вправо
- transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
- }
- else
- {
- // Поворот влево
- transform.localScale = new Vector3(-Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
- }
- }
- private void OnCollisionEnter2D(Collision2D collision)
- {
- // Проверка на столкновение с игроком
- if (collision.gameObject.CompareTag("Player"))
- {
- // Нанесение урона игроку
- collision.gameObject.GetComponent<Player2DControl>().TakeDamage(damage);
- }
- }
- // Метод для нанесения урона врагу
- public void TakeDamage(int damageAmount)
- {
- health -= damageAmount;
- if (health <= 0)
- {
- // Враг умер
- Destroy(gameObject);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement