Advertisement
Guest User

Untitled

a guest
Feb 9th, 2021
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.98 KB | None | 0 0
  1. public class Enemies : MonoBehaviour
  2. {
  3.     public Transform player;
  4.     private Rigidbody2D rb;
  5.     private Vector2 movement;
  6.     public float speed = 5f;
  7.     public int health;
  8.     // Start is called before the first frame update
  9.     void Start()
  10.     {
  11.         rb = this.GetComponent<Rigidbody2D>();
  12.     }
  13.  
  14.     // Update is called once per frame
  15.     void Update()
  16.     {
  17.         Vector2 direction = player.position - transform.position;
  18.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
  19.         rb.rotation = angle;
  20.         direction.Normalize();
  21.         movement = direction;
  22.  
  23.     }
  24.     private void FixedUpdate()
  25.     {
  26.         Move(movement);
  27.     }
  28.     void Move(Vector2 direction)
  29.     {
  30.         rb.MovePosition((Vector2)transform.position + (direction * speed * Time.deltaTime));
  31.     }
  32.     private void OnMouseDown()
  33.     {
  34.         health = health - 1;
  35.         if(health <= 0)
  36.         {
  37.             Destroy(gameObject);
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement