Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Move GameObject from WayPoint to WayPoint by giving Input

Discussion in 'Scripting' started by Richard_Kogler, Jan 3, 2023.

  1. Richard_Kogler

    Richard_Kogler

    Joined:
    Apr 29, 2022
    Posts:
    4
    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.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Something like...

    Code (CSharp):
    1. public Transform[] waypoints;
    2. public float speed = 5;
    3. int waypointIndex = 0;
    4.  
    5. void Update() {
    6.   if (waypoints.Length < 1) return;
    7.  
    8.   if (Input.GetKeyDown(KeyCode.D)) {
    9.     waypointIndex = (waypointIndex + 1) % waypoints.Length;
    10.   }
    11.  
    12.   Transform target = waypoints[waypointIndex];
    13.   transform.position = Vector3.MoveTowards(transform.position, target.position, Time.deltaTime * speed);
    14. }
     
    Last edited: Jan 3, 2023
  3. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    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).
     
    Last edited: Jan 3, 2023
    Richard_Kogler likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Whoa, that was really nice and compact. Respect to you!

    Bonus! It actually works!
     
    Last edited: Jan 3, 2023
    Richard_Kogler and PraetorBlue like this.
  5. Richard_Kogler

    Richard_Kogler

    Joined:
    Apr 29, 2022
    Posts:
    4
    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.
     
  6. Richard_Kogler

    Richard_Kogler

    Joined:
    Apr 29, 2022
    Posts:
    4
    This code works for me.


    Code (CSharp):
    1.  
    2. public class MoveToWayPoint : MonoBehaviour
    3. {
    4.     public Transform[] wayPoints;
    5.  
    6.     public float speed = 5;
    7.  
    8.     int waypointIndex = 0;
    9.  
    10.     private bool isMoving = false;
    11.  
    12.  
    13.     void Update()
    14.     {
    15.         if (wayPoints.Length < 1) return;
    16.  
    17.         if (Input.GetKeyDown(KeyCode.W) && !isMoving)
    18.         {
    19.             waypointIndex = (waypointIndex + 1) % wayPoints.Length;
    20.             isMoving = true;
    21.         }
    22.  
    23.         MoveToNextWayPoint();
    24.  
    25.     }
    26.  
    27.     private void MoveToNextWayPoint()
    28.     {
    29.  
    30.         Transform target = wayPoints[waypointIndex];
    31.         transform.position = Vector3.MoveTowards(transform.position, target.position, Time.deltaTime * speed);
    32.  
    33.         if(transform.position == wayPoints[waypointIndex].transform.position)
    34.         {
    35.             isMoving = false;
    36.         }
    37.  
    38.         if(waypointIndex == wayPoints.Length)
    39.         {
    40.             waypointIndex = 0;
    41.         }
    42.     }
    43. }
    44.  
     
    cam415 likes this.
  7. Richard_Kogler

    Richard_Kogler

    Joined:
    Apr 29, 2022
    Posts:
    4
    Again thanks guys, really helped me a lot.
     
    Kurt-Dekker likes this.