Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Scripts / NPC / Ranged Slime / RangedSlimeMovement.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class RangedSlimeMovement : MonoBehaviour
  5. {
  6. //missiong the change of direction for the animations
  7. private GameObject target; // player
  8. public float moveForce = 2;
  9. public float maxrange = 9;
  10. public float minrange = 0.75f; // so that the enemy doesn't push the player
  11. private Vector3 homePosition;
  12. void Start()
  13. {
  14. homePosition = transform.position;
  15. target = GameObject.FindGameObjectWithTag("Player");
  16. }
  17. void Update()
  18. {
  19. if (Vector3.Distance(transform.position, target.transform.position) <= maxrange && Vector3.Distance(transform.position, target.transform.position) >= minrange)
  20. {
  21. FollowPlayer();
  22. }
  23. else if (Vector3.Distance(transform.position, target.transform.position) >= maxrange)
  24. {
  25. GoStartingPos();
  26. }
  27. }
  28. public void FollowPlayer()
  29. {
  30. transform.position = Vector3.MoveTowards(transform.position, target.transform.position, moveForce * Time.deltaTime);
  31. }
  32. public void GoStartingPos()
  33. {
  34. transform.position = Vector3.MoveTowards(transform.position, homePosition, moveForce * Time.deltaTime);
  35. }
  36. }