简体   繁体   中英

Grid-based/Tile Movement + Collisions in Unity?

Having a huge problem figuring out how to do a grid-based movement or tile based movement. Somewhat similar to Nitrome's Redungeon: Can't really post an image: so here's a gif. http://www.eneminds.com/redungeon/img/gifs/gif_1.gif

I figured I should create a grid system. I thought this was easy just like the old games(Pokemon etc.) Not sure how to do that though. But I also want the movement to be fast.

Unity has its own implementation of tilemaps which should allow you to build the background aka the ground you walk on: https://docs.unity3d.com/Manual/Tilemap.html

Furthermore, Unity provides more 2d content to help you build things right here: https://github.com/Unity-Technologies/2d-extras

Collisions are outlined in numerous tutorials found on here: https://unity3d.com/learn/tutorials/s/2d-game-creation

To make a game like the one in the animated GIF, is not easy . This is especially the case if you're new to game dev, so don't expect easy solutions.

You might want to check out the Unity Asset Store for a quick 'n dirty way of getting tile-based movement, but this as always will come with a price and will not help you learn. https://assetstore.unity.com/

With regards to movement speed, this is always just a variable in your game which can be tuned to your preference. In the case of a tile based game it would correspond to the time it takes for the character to be visually shifted from one tile to another. Try reading up on Lerping which is a part of the Unity engine. https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

I was able to pull it off. I just modified the input a little from the RogueLike Tutorial from Unity's Learn Section.

So This is the code responsible for movement

    void Move(string direction_string)
    {
        ChangeDirection(direction_string);

        Vector2 start = transform.position;
        Vector2 end = start + direction;

        boxCollider.enabled = false;
        hit = Physics2D.Linecast(start, end, blockingLayer);
        boxCollider.enabled = true;

        if (hit.transform == null) {
            StartCoroutine(Movement(end));
            animator.SetTrigger(direction_string);
            return;
        } else {
            moveSequence.RemoveAt(0);
            animator.SetTrigger(direction_string);
            return;
        }
    }

    void ChangeDirection(string direction_string)
    {
        switch (direction_string) {
            case "up":
                direction = dir_Up;
                break;
            case "down":
                direction = dir_Down;
                break;
            case "left":
                direction = dir_Left;
                break;
            case "right":
                direction = dir_Right;
                break;
        }
    }

    IEnumerator Movement(Vector3 end)
    {
        moving = true;
        float sqrRemainingDistance = (transform.position - end).sqrMagnitude;

        while (sqrRemainingDistance > float.Epsilon) {
            Vector3 newPosition = Vector3.MoveTowards(rb2D.position, end, moveSpeed * Time.deltaTime);
            rb2D.MovePosition(newPosition);
            sqrRemainingDistance = (transform.position - end).sqrMagnitude;
            yield return null;
        }

        currentPos = end;
        moveSequence.RemoveAt(0);
        moving = false;
    }
}

Here is what's responsible for input


    void FixedUpdate ()
    {
        if (moveSequence.Count > 0 && !moving) {
            Move(moveSequence[0]);
        }
    }

Then just hook it up to an Update() function that listens to button press and add a list item to moveSequence List like

moveSequence.Add("up");

Just did a code like this for someone on reddit, came here to see if its been asked before. I know I'm 2 years late but here it goes in case anyone needs it.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
     
    public class GameController : MonoBehaviour
    {
     
        // a script created so that a player always moves one direction at a time on a grid.
        // It uses a plane with even width and height to make a grid
        //     - if needed, turn off the mesh renderer so that you dont see the tiled grid.
     
     
     
     
        // the game object we use to create a grid. Should be a plane with an even width and height.
        public  GameObject tile_object;
     
        // grid x and y sizes, set them to odd numbers
        public float x_grid_size, y_grid_size;
     
        // List of all our floor tiles
        Dictionary<(float x, float y), GameObject> floor_list = new Dictionary<(float x, float y), GameObject>();
     
        // Reference to the player prefab
        public GameObject player;
     
        // reference to player character
        private GameObject player_character;
     
        // a tool used for debugging
        bool debugging = true;
     
        //check if player is moving
        bool player_is_moving = false;
     
        (float x, float y) players_current_tile;
     
        // players speed
        public float player_speed = 35f;
     
        // used in calculating movements
        (float x, float y) to_tile;
     
        //used to calculate movement
        Vector3 to_pos;
     
     
        // Start is called before the first frame update
        void Start()
        {
            // put the player into the scene
           player_character = Instantiate(player, Vector3.zero, player.transform.rotation);
     
            // Sets the Middle Tile and checks grid is odd
            SetMiddleTile();
     
            // create a floor grid to help us visualize our play field.
            CreateGrid();
     
        }
     
        // Update is called once per frame
        void Update()
        {
            PlayerMovement();
            MovePlayer();
        }
     
        // gets inputs on A S W D keys for movement
        void PlayerMovement() {
            if (Input.GetKeyDown(KeyCode.W)){
                PlayerInput((0, 1));
            }
            if (Input.GetKeyDown(KeyCode.A))
            {
                PlayerInput((-1, 0));
            }
            if (Input.GetKeyDown(KeyCode.S))
            {
                PlayerInput((0, -1));
            }
            if (Input.GetKeyDown(KeyCode.D))
            {
                PlayerInput((1, 0));
            }
        }
     
     
        // simplifies player entries, and gets input directional data
        void PlayerInput((float x, float y) input)
        {
            if (!player_is_moving)
            {
                (float x, float y) to_tile_check = (players_current_tile.x + input.x, players_current_tile.y + input.y);
                if (floor_list[to_tile_check] != null)
                {
                    player_is_moving = true;
     
                    to_tile = (players_current_tile.x + input.x, players_current_tile.y + input.y);
                    to_pos = floor_list[to_tile].transform.position;
                }
            }
     
        }
     
       // used to move the player
        void MovePlayer()
        {
     
            if (player_is_moving)
            {
                player_character.transform.position = Vector3.MoveTowards(player_character.transform.position, to_pos, Time.deltaTime * player_speed);
     
                if (player_character.transform.position == to_pos)
                {
                    players_current_tile = to_tile;
                    player_is_moving = false;
                }
            }
        }
     
        // used to create the floor grid
        void CreateGrid()
        {
            // Create an empty to hold our floor grid
            GameObject grid_empty = new GameObject();
            grid_empty.name = "grid_floor";
     
            // get the size of our floor tile so we know how far to space them.
            float tile_size = tile_object.GetComponent<MeshRenderer>().bounds.size.x;
     
            // create an offset so the grid is always centered in game view
            Vector3 offSet = new Vector3((x_grid_size * tile_size) / 2 - (tile_size / 2), 0, (y_grid_size * tile_size)/2 - (tile_size / 2));
     
            // iterate to create tiles on x axis
            for (int x = 0; x < x_grid_size; x++)
            {
                // iterate to create tiles on y axis
                for (int y = 0; y < y_grid_size; y++)
                {
                    // instantiate new tile
                    GameObject floor_tile = Instantiate(tile_object, (new Vector3(x * tile_size, 0, y * tile_size) - offSet), tile_object.transform.rotation);
                    floor_tile.name = "flr_x: " + (x  + 1) + " y:" + (y + 1);
     
                    // set the parent to grid empty so the scene isnt filled with objects and all floor tiles are neatly centerd
                    floor_tile.transform.parent = grid_empty.transform;
     
                    // add the tile to our dictionary.
                    floor_list.Add(((x + 1),(y + 1)), floor_tile);
                }
            }
            if (debugging) { print("Created Floors: " + (x_grid_size * y_grid_size) + " Floor List Size: " + floor_list.Count);}
        }
     
        // sets the middle tile the player is spawned on, also checks to make sure grid size is set correctly
       void SetMiddleTile()
        {
            // these check to make sure grid size isnt 0 and are odd
            if (x_grid_size == 0 && y_grid_size == 0) { print("Forgot to set grid size! setting default to 7x and 7y"); x_grid_size = 7; y_grid_size = 7; }
            if (x_grid_size % 2 == 0) { print("x_grid_size is set to an even number(" + x_grid_size + "), changing it to odd(" + (x_grid_size + 1) + ")"); x_grid_size += 1; }
            if (y_grid_size % 2 == 0) { print("y_grid_size is set to an even number(" + y_grid_size + "), changing it to odd(" + (y_grid_size + 1) + ")"); y_grid_size += 1; }
     
            // splits the grid and half
            float x = x_grid_size / 2 + 0.5f;
            float y = y_grid_size / 2 + 0.5f;
     
            // set the players current tile to middle tile
            players_current_tile = (x, y);
     
            // set the to tile to current tile to avoid null instances when first checking our dictionary
            to_tile = players_current_tile;
     
            // used for debugging
            if (debugging) { print("the middle tile is: x(" + x + ")" + " y(" + y + ")"); }
        }
    }
         

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM