UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"control the movement of player using waypoints system"
public class PlayerControl : MonoBehaviour { [SerializeField] Transform[] waypoints; [SerializeField] float moveSpeed = 2f; int waypointIndex = 0; void Start () { transform.position = waypoints [waypointIndex].transform.position; } void Update () { Move (); } void Move() { transform.position = Vector2.MoveTowards (transform.position, waypoints[waypointIndex].transform.position, moveSpeed * Time.deltaTime); if (transform.position == waypoints [waypointIndex].transform.position) { waypointIndex = 1; } if (waypointIndex == waypoints.Length) waypointIndex = 0; } }