Advertisement
fuccpuff

Untitled

Nov 24th, 2023 (edited)
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class EnemyFollow : MonoBehaviour
  4. {
  5. public Transform player; // Ссылка на трансформ игрока
  6. public float speed = 5f; // Скорость врага
  7. public int health = 100; // Здоровье врага
  8. public int damage = 10; // Урон, наносимый игроку
  9.  
  10. private void Update()
  11. {
  12. // Направление от врага к игроку
  13. Vector3 direction = (player.position - transform.position).normalized;
  14.  
  15. // Движение врага
  16. transform.position += direction * speed * Time.deltaTime;
  17.  
  18. // Поворот врага влево или вправо
  19. if (direction.x > 0)
  20. {
  21. // Поворот вправо
  22. transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
  23. }
  24. else
  25. {
  26. // Поворот влево
  27. transform.localScale = new Vector3(-Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
  28. }
  29. }
  30.  
  31. private void OnCollisionEnter2D(Collision2D collision)
  32. {
  33. // Проверка на столкновение с игроком
  34. if (collision.gameObject.CompareTag("Player"))
  35. {
  36. // Нанесение урона игроку
  37. collision.gameObject.GetComponent<Player2DControl>().TakeDamage(damage);
  38. }
  39. }
  40.  
  41. // Метод для нанесения урона врагу
  42. public void TakeDamage(int damageAmount)
  43. {
  44. health -= damageAmount;
  45. if (health <= 0)
  46. {
  47. // Враг умер
  48. Destroy(gameObject);
  49. }
  50. }
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement