if (waypoints == null || waypoints.Length == 0) return;
Transform targetWaypoint = waypoints[currentWaypointIndex];
Vector2 direction = (targetWaypoint.position - transform.position).normalized;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion targetRotation = Quaternion.Euler(new Vector3(0, 0, angle));
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
rb.MovePosition(rb.position + direction * speed * Time.deltaTime);
if (Vector2.Distance(transform.position, targetWaypoint.position) < 0.1f)
{
currentWaypointIndex++;
if (currentWaypointIndex >= waypoints.Length)
{
currentWaypointIndex = 0;
}
}
This is the movement script
enter image description here
canvas setting
solve method and reasons
New contributor
Kryze is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1