Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Scripts / NPC / Quest NPC / QuestNPCMovement.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class QuestNPCMovement : MonoBehaviour
  5. {
  6. private Rigidbody2D rb;
  7. private RangedArea rangedArea;
  8. [SerializeField] private float moveForce = 1f;
  9. private Vector3[] positionArray;
  10. private int pointsIndex;
  11. // Start is called before the first frame update
  12. void Start()
  13. {
  14. rangedArea = FindObjectOfType<RangedArea>();
  15. rb = GetComponent<Rigidbody2D>();
  16. positionArray = new[] {new Vector3(6f, -10.5f), new Vector3(18.5f, -10.5f),
  17. new Vector3(18.5f, 8.5f)};
  18. pointsIndex = 0;
  19. }
  20. // Update is called once per frame
  21. void FixedUpdate()
  22. {
  23. if (rangedArea.playerInRange == false)
  24. {
  25. transform.position = Vector3.MoveTowards(transform.position, positionArray[pointsIndex], moveForce * Time.deltaTime);
  26. if (transform.position == (positionArray[pointsIndex]))
  27. {
  28. //Next point of the array of Locations
  29. pointsIndex++;
  30. }
  31. if (pointsIndex == (positionArray.Length))
  32. {
  33. //Going Back to the start point
  34. pointsIndex = 0;
  35. }
  36. }
  37. }
  38. }