Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jacobw56 · Dec 12, 2018 at 06:16 PM · prefabspritetilemaptiletiles

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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>();

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jacobw56 · Dec 12, 2018 at 07:25 PM 0
Share

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.

avatar image Vega4Life jacobw56 · Dec 12, 2018 at 08:54 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

149 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Get individual tile under a Tilemap Collider 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges