Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to make grid based movement with Unity 2d

Discussion in 'Game Design' started by tobbe50cent, May 11, 2023.

  1. tobbe50cent

    tobbe50cent

    Joined:
    May 5, 2021
    Posts:
    1
    Hello. I've been making a game for some time now which is a top down maze game. I want to make a grid-based movement system, where you move exactly one tile when moving.

    I want the movemen system to be pretty much like this game:
    https://www.disney--games.com/pizza_party_pickup_283.html

    This is what I have been trying so far:


    Code (CSharp):
    1.     void Start()
    2.     {
    3.         sfe = GetComponent<SimpleFlashExample>();
    4.         flames[0] = theFlameThrower;
    5.         flames[1] = flameUp;
    6.         flames[2] = flameLeft;
    7.         flames[3] = flameRight;
    8.         foreach(GameObject flame in flames)
    9.         {
    10.             flame.SetActive(false);
    11.         }
    12.         source = GetComponent<AudioSource>();
    13.         bs = Camera.main.GetComponent<BasicCamera>();
    14.         isAffected = false;
    15.         myRigidbody = GetComponent<Rigidbody2D>();
    16.         myAnim = GetComponent<Animator>();
    17.         shield.SetActive(false);
    18.         playerSprite = GetComponent<SpriteRenderer>();
    19.         myTransform = GetComponent<Transform>();
    20.         gs = FindObjectOfType<GameSession>();
    21.    
    22. }
    23.  
    24.     public float tileSize = 1;
    25.  
    26.     void Update()
    27.     {
    28.         playerMoving = false;
    29.         //if (isFiring)
    30.         //{
    31.         //    foreach (GameObject flame in flames)
    32.         //    {
    33.  
    34.  
    35.         //        flame.SetActive(false);
    36.  
    37.  
    38.         //    }
    39.         //    isFiring = false;
    40.         //}
    41.  
    42.         if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.5f && Input.GetAxisRaw("Vertical") == 0f)
    43.         {
    44.             // Only move horizontally
    45.             Vector3 movement = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f) * tileSize;
    46.             transform.Translate(movement * speed * Time.deltaTime);
    47.             playerMoving = true;
    48.             lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
    49.         }
    50.         else if (Mathf.Abs(Input.GetAxisRaw("Vertical")) > 0.5f && Input.GetAxisRaw("Horizontal") == 0f)
    51.         {
    52.             // Only move vertically
    53.             Vector3 movement = new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f) * tileSize;
    54.             transform.Translate(movement * speed * Time.deltaTime);
    55.             playerMoving = true;
    56.             lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
    57.         }
    58.  
    59.         //Set shoot direction:
    60.         if (Input.GetKeyDown(KeyCode.LeftArrow))
    61.         {
    62.             shootDirection = "Left";
    63.         }
    64.  
    65.         else if (Input.GetKeyDown(KeyCode.RightArrow))
    66.         {
    67.             shootDirection = "Rigth";
    68.         }
    69.  
    70.         else if (Input.GetKeyDown(KeyCode.DownArrow))
    71.         {
    72.             shootDirection = "Down";
    73.         }
    74.  
    75.         else if (Input.GetKeyDown(KeyCode.UpArrow))
    76.         {
    77.             shootDirection = "Up";
    78.         }
    79.  
    80.  
    81.         //--------------------------
    82.         myAnim.SetFloat("moveX", Input.GetAxisRaw("Horizontal"));
    83.         myAnim.SetFloat("moveY", Input.GetAxisRaw("Vertical"));
    84.         myAnim.SetBool("PlayerMoving", playerMoving);
    85.         myAnim.SetFloat("lastmoveX", lastMove.x);
    86.         myAnim.SetFloat("lastmoveY", lastMove.y);
    My character is 32 by 32 pixels and should move on a level where each tile is 32 by 32 pixels.
    He should be able to move as long as a key is being pressed down and only stop once you lift the key.

    What could I do in order to make it more real grid-based. None of the videos I have found helps at all.

    Should I adjust the colliders of the wall, the grid size or the player speed?
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,576
    That's not really grid-based. If you want real grid-based movement than when you release the key, the player character should continue walking until they settle in to the next grid space and then stop.