Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Enemies : MonoBehaviour
- {
- public Transform player;
- private Rigidbody2D rb;
- private Vector2 movement;
- public float speed = 5f;
- public int health;
- // Start is called before the first frame update
- void Start()
- {
- rb = this.GetComponent<Rigidbody2D>();
- }
- // Update is called once per frame
- void Update()
- {
- Vector2 direction = player.position - transform.position;
- float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
- rb.rotation = angle;
- direction.Normalize();
- movement = direction;
- }
- private void FixedUpdate()
- {
- Move(movement);
- }
- void Move(Vector2 direction)
- {
- rb.MovePosition((Vector2)transform.position + (direction * speed * Time.deltaTime));
- }
- private void OnMouseDown()
- {
- health = health - 1;
- if(health <= 0)
- {
- Destroy(gameObject);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement