Move GameObject from WayPoint to WayPoint by giving Input

Hi!
I´m pretty sure you guys can help me.
I try to move the player on an WorldMap like in Mario Bros 3 or Super Mario World.
I want to move the player from 1 WayPoint or GameObject (Level or Event), after the interaction is done, to the next one.
My only problem is the movement.
If you press, in my case “D” on the Keyboard once, it should move to the next location and so on.

Something like…

public Transform[] waypoints;
public float speed = 5;
int waypointIndex = 0;

void Update() {
  if (waypoints.Length < 1) return;

  if (Input.GetKeyDown(KeyCode.D)) {
    waypointIndex = (waypointIndex + 1) % waypoints.Length;
  }

  Transform target = waypoints[waypointIndex];
  transform.position = Vector3.MoveTowards(transform.position, target.position, Time.deltaTime * speed);
}
4 Likes

Let’s think about this at a high level:

  • The player can be in one of two states

  • Not moving

  • Moving

  • If the player is not moving, then…

  • They are at a specific place

  • They can push a key to start moving (but only some directions are valid)

  • If the player is moving, then…

  • They are between two places

  • They can’t do anything

I would model each “place” as a GameObject with a component on it. Call it Place. That component should store references to other instances of Place: one for each cardinal direction.

The player should have two Place variables: src and dst. src is where the player currently is, and dst, if not null, is where the player is moving to.

If dst is null, the player is not moving. When they push a button, dst is set to the corresponding Place, and a counter is set to the duration of the move. Let’s go with 1.

If dst is not null, the player is moving. Decrement the counter by Time.deltaTime. Then, set the player’s position to be somewhere between the source and destination positions (check out Vector3.Lerp!).

Whenever the player finishes moving – because the counter is at 0 – set src to dst, then set dst to null. The player is now no longer moving!

That’s how I’d approach the problem. Define your states (moving, not moving), define what each states does (wait for input, move the player), and define how to move between states (pushing a key, reaching the destination).

1 Like

Whoa, that was really nice and compact. Respect to you!

Bonus! It actually works!

2 Likes

Thank you for your reply.
I haven´t it figured out yet, solution from PraetorBlue is pretty good and understandable but if you press Input 2 times it moves 2 points.
I guess chemicalcrux answer is what I´m searching for.
I will leave my code here when i got it.
Thanks guys you helped me a lot.

This code works for me.

public class MoveToWayPoint : MonoBehaviour
{
    public Transform[] wayPoints;

    public float speed = 5;

    int waypointIndex = 0;

    private bool isMoving = false;


    void Update()
    {
        if (wayPoints.Length < 1) return;

        if (Input.GetKeyDown(KeyCode.W) && !isMoving)
        {
            waypointIndex = (waypointIndex + 1) % wayPoints.Length;
            isMoving = true;
        }

        MoveToNextWayPoint();

    }

    private void MoveToNextWayPoint()
    {

        Transform target = wayPoints[waypointIndex];
        transform.position = Vector3.MoveTowards(transform.position, target.position, Time.deltaTime * speed);

        if(transform.position == wayPoints[waypointIndex].transform.position)
        {
            isMoving = false;
        }

        if(waypointIndex == wayPoints.Length)
        {
            waypointIndex = 0;
        }
    }
}
1 Like

Again thanks guys, really helped me a lot.

1 Like