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 Ecir_Leumas · May 24, 2020 at 02:41 PM · unity 2dtilemaptiles

How can I get the world position of a Tile?

Is there a method to get the world position of a tile? I have randomly generating platforms, and I want the next platform to spawn somewhere after the last tile of the previous one. The platform's length is also varied. Is there a way to the world position of a tile, in a tilemap? Thank you and have a good day.

Comment
Add comment · Show 3
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 jaydenvanvliet9 · May 24, 2020 at 02:43 PM 0
Share

Do you mean the position? or what do you mean exactly?

avatar image Ecir_Leumas · May 24, 2020 at 02:48 PM 0
Share

@jaydenvanvliet9 Yeah I mean the position. I wanted to get a tile, and make a transform's position that tile's world position.

avatar image jaydenvanvliet9 Ecir_Leumas · May 24, 2020 at 02:49 PM 0
Share

I think its just the position at the inspector.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SteenPetersen · May 24, 2020 at 03:08 PM

Hi @Ecir_Leumas

It would make it a great deal easier for us to help with your question if you took the time to be a bit more specific with posing the problem. There are hundreds of ways to get the position of things in Unity and which way would be very dependant on what your problem actually is. Without that information it is very difficult to come with any intelligent reply to help.

That being said if you can get a reference to the tile in question, then every single object in Unity has a transform. Therefore you can simply say;

 // do something to find a reference to the tile e.g.
 GameObject tile = GameObject.Find("myTile");
 
 Vector3 tilesPosition = tile.transform.position;


But using GameObject.Find is not a very efficient way to do this, so expanding your explanation of what the problem is, would greatly help the community, help you.

Comment
Add comment · Show 1 · 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 Ecir_Leumas · May 24, 2020 at 03:20 PM 0
Share

I'll edit my post. Thanks.

avatar image
0

Answer by enerology · May 25, 2020 at 05:34 AM

Instead of getting that tile's position, you can store the last tiles placement position + the length of the platform to find the ending position. From there you can do your calculation as that would be the ending tile to the platform and ill write pseudo code to give a reference.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.Tilemaps;
 
 public class platformGeneratorController : Monobehavior
 {
     private Vector2 lastPlatformPosition;
     public Tilemap myTilemap;
     public Tile platformTile;
 
     public void generatePlatform()
     {
         //Random height from origin of world
         //If you want it from last tile's placed position, then add lastPlatformPosition.y to the random height section in SetTile();
         int randomHeight = Random.Range(-5, 6);
         //How far this platform will spawn from the last one
         int randomDistanceAway = Random.Range(0, 5);
         //The length of the platform
         int randomLength = Random.Range(2, 5);
 
         if (lastPlatformPosition != null) {
             for (int i = 0; i < randomLength; i++)
             {
                 //For loop increments the position.x and places a tile at every incrementation for the randomLength
                 myTilemap.SetTile(new Vector3Int((int)(i + randomDistanceAway + lastPlatformPosition.x), randomHeight, 0), platformTile);
             }
         }
         else
         {
             //This is called if it is the first tile
             for (int i = 0; i < randomLength; i++)
             {
                 myTilemap.SetTile(new Vector3Int(i + randomDistanceAway, randomHeight, 0), platformTile);
             }
         }
 
         //This is the end tile position of the last platform placed
         lastPlatformPosition = new Vector2(randomDistanceAway + lastPlatformPosition.x + randomLength, randomHeight);
         
     }
 }

I hope this helps and good luck with your project!

Comment
Add comment · 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

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

139 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

Related Questions

How would I instantiate rooms made of a ruletile as part of a rogue-like random level generation system and have the ruletile affected by the other spawned rooms rule tiles? 0 Answers

TileMap won't show up in 2d object in heirarchy. 0 Answers

My character is floating. How do I fix this? 1 Answer

Best way to, change world tileset/tile pallet 0 Answers

[Solved] Isometric Rule Tiles not Following Guidelines. 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