Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Basic Grid Movement Confusion

Discussion in 'Scripting' started by domilab, Sep 22, 2014.

  1. domilab

    domilab

    Joined:
    Mar 23, 2013
    Posts:
    13
    I'm having some issues implementing simple grid movement. I've tried several different methods lerp, translate, and moveTowards without any luck. In every instance it just doesn't work or it moves the player. I'll post code snippets with descriptions down below.

    The game is a top down game where the player is restrained to a grid. I have most of the code written; the inputs, testing if the player is able to move to the square, animations, etc.. I just can't get a smooth transition from one square to the next. I need to be able to call MovePlayer or something of that nature, the player transitions over a set amount of time, once that transition ends, it needs to recheck input and see if the button is held, if not, it sets isMoving to false.

    Any help is appreciated. I'm really having a hard time wrapping my head around this.

    My efforts:

    The compiler tells me I can't have this code in the update because it's of type void(I found it in another post):
    Code (CSharp):
    1.     private IEnumerator  MoveObject () {
    2.         float i = 0.0f;
    3.         float rate = 1.0f/speed;
    4.         while (i < 1.0f) {
    5.             i += Time.deltaTime * rate;
    6.             transform.position = Vector3.Lerp(transform.position, transform.position + Vector3.up, i);
    7.             yield break;
    8.         }
    9.     }
    This is just teleporting it as I expected:
    Code (CSHARP):
    1. transform.Translate (.5f, 0, 0);
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Should be yield return null. If you use break then it breaks out of the loop. Also the start and end positions for Lerp should be defined outside the loop, otherwise it will feed back on itself and add the position cumulatively every frame. And no, that code can't be in Update nor would you want it to be. Update is only for code that runs every single frame forever, and the coroutine will stop once the movement is over.

    --Eric
     
  3. domilab

    domilab

    Joined:
    Mar 23, 2013
    Posts:
    13
    Eric,
    Thank you for the reply. It definitely got me going in the right direction, especially with the feedback. I'll post my code below. Would this cause easing? It almost seems to slow down the closer it gets to the target.

    Code (CSharp):
    1.     private IEnumerator  MoveObject (Transform player, Vector3 endPoint, float speed) {
    2.         float i = 0.0f;
    3.         while (i < 1.0f) {
    4.             i += Time.deltaTime * speed;
    5.             transform.position = Vector3.Lerp(player.position, endPoint, i);
    6.             yield return null;
    7.         }
    8.         float xDir = Input.GetAxis ("Horizontal");
    9.         float yDir = Input.GetAxis ("Vertical");
    10.  
    11.         //see if the move button was let go.  If it was stop
    12.         if (xDir == 0 && yDir == 0) {
    13.             animator.SetBool("isMoving", false);
    14.         }
    15. else
    16. {
    17.         DoMovement ();
    18. }
    19. }
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yes, the start and end positions should be defined outside the loop.

    --Eric
     
  5. domilab

    domilab

    Joined:
    Mar 23, 2013
    Posts:
    13