- Home /
How to create tile prefabs?
I am working through a simple Rogue-Like 2D Tutorial and have been experimenting adding on my own ideas.
I thought I would add functionality for the character to be able to drop items (bombs, traps, etc) depending on which key the player presses (B for bomb, I for ice trap, etc). I am trying to do it like this (my Player controller script):
public class PlayerController : MonoBehaviour {
// Tiled movement
private Vector3 speedX = new Vector3(1.0f, 0, 0);
private Vector3 speedY = new Vector3(0, 1.0f, 0);
// Add this to the grid coordinates to get the actual position of the cell
private Vector3 positionBuffer = new Vector3(0.5f, 0.5f, 0);
private Vector3 startPosition;
// Base grid coord on which to base keyed movement
private Vector3Int startCoord;
// Grid/Tilemap objects
private Grid grid;
private Tilemap wallTiles;
private Tilemap floorTiles;
private Tilemap overlayTiles;
[SerializeField] private GameObject BombPrefab;
private Tile _bomb;
// Use this for initialization
void Start ()
{
grid = FindObjectOfType<Grid>();
wallTiles = GameObject.Find("wallTiles").GetComponent<Tilemap>();
floorTiles = GameObject.Find("floorTiles").GetComponent<Tilemap>();
overlayTiles = GameObject.Find("CellOverlays").GetComponent<Tilemap>();
_bomb = new Tile();
}
// Update is called once per frame
void Update ()
{
// Starting position and coords are used as base for keyed movement
startPosition = transform.position;
startCoord = grid.WorldToCell(startPosition);
// Keep the player from spinning about
transform.rotation = Quaternion.Euler(0, 0, 0);
// Probably a better way to abstract this behavior... lots of repeated code here
// Don't move into wall tiles
// RigidBody & Collider causes player to "bounce" off the grid - don't use
if (Input.GetKeyDown ("left"))
{
if (wallTiles.GetTile(grid.WorldToCell(startPosition - speedX)) == null)
{
transform.position = startCoord - speedX + positionBuffer;
}
}
else if (Input.GetKeyDown ("right"))
{
if (wallTiles.GetTile(grid.WorldToCell(startPosition + speedX)) == null)
{
transform.position = startCoord + speedX + positionBuffer;
}
}
else if (Input.GetKeyDown ("up"))
{
if (wallTiles.GetTile(grid.WorldToCell(startPosition + speedY)) == null)
{
transform.position = startCoord + speedY + positionBuffer;
}
}
else if (Input.GetKeyDown ("down"))
{
if (wallTiles.GetTile(grid.WorldToCell(startPosition - speedY)) == null)
{
transform.position = startCoord - speedY + positionBuffer;
}
}
// Mouse click
else if (Input.GetMouseButtonDown(0))
{
// Get click/press world position then cast to tilemap coordinates
Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3Int mouseCoordinate = grid.WorldToCell(mouseWorldPos);
// Only move player position if the tile is in the actual
// game grid and is not a wall
if ((wallTiles.GetTile(mouseCoordinate) == null) && (floorTiles.GetTile(mouseCoordinate) != null))
{
transform.position = mouseCoordinate + positionBuffer;
}
}
// Lay bomb on "B" key press
else if (Input.GetKeyDown ("b"))
{
_bomb.sprite = Instantiate(BombPrefab).GetComponent<Sprite>();
overlayTiles.SetTile(startCoord, _bomb);
}
// Screen touch
else if (Input.touchCount == 1)
{
// Get click/press world position then cast to tilemap coordinates
Touch touch = Input.GetTouch(0);
Vector3 touchWorldPos = Camera.main.ScreenToWorldPoint(touch.position);
Vector3Int touchCoordinate = grid.WorldToCell(touchWorldPos);
// Only move player position if the tile is in the actual
// game grid and is not a wall
if ((wallTiles.GetTile(touchCoordinate) == null) && (floorTiles.GetTile(touchCoordinate) != null))
{
transform.position = touchCoordinate + positionBuffer;
}
}
}
}
However the bomb tile is always placed at (0, 0, 0) (or whatever the position of the prefab Transform is) and not under the player. I feel pretty sure I am overlooking something obvious, but I don't see how to get the sprite to "stick" to the tile itself.
Is there a resource somewhere that covers creating Tile prefabs and how to apply them in script?
Answer by Vega4Life · Dec 12, 2018 at 07:15 PM
Add in the position where you want the bomb to spawn - in this case your players position:
_bomb.sprite = Instantiate(BombPrefab, transform.position, Quaternion.identity).GetComponent<Sprite>();
Well I definitely get it (and thank you very much!) but is there some obvious reason why applying the sprite to the tile object isn't independent of that position? In my head I see it like applying a sticker to a blank tile, then moving that tile where ever I want. But clearly the sticker itself has a position in this case, so if I'm not careful about where I hold the tile as I apply it, it will end up stuck on the floor a few rooms over.
Um, I think some wordings are confusing things. You create a gameObject which holds position, scale, rotation, and components potentially. In your case, the gameObject has a spriteRenderer component on it. The sprite has no positional value on its on, it's based on the gameObjects (0,0,0 for you). Everything in unity (at least in the scene) is based on gameObjects.
When we instantiate them, we create a copy and place them in their saved positions - that goes for everything on them. So, I wouldn't think of moving a Sprite around, we should think of moving gameObjects around.
Your answer
Follow this Question
Related Questions
Correct use of setTile to add tile to Tilemap 0 Answers
Having trouble with dragging sprites into the tile palette 1 Answer
How do I make the Collider match the sprite? 2 Answers
tilepalette problems 0 Answers