刚刚在 reddit 上为某人做了一个这样的代码,来这里看看它之前是否被问过。我知道我迟到了 2 年,但如果有人需要,它就在这里。
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 + ")"); }
}
}