Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 rainbowbeansprout · Jan 20, 2020 at 12:41 AM · transform.positiontilemaplocalpositionworld coordinates

World coordinates conversion to cell coordinates in tilemap

I'm trying to manually set tiles in a tilemap within my script, and then position gameobjects on-screen relative to those tiles. I'm running into an issue where converting between cell coordinates and world position of a mouse press (Input.mousePosition) just returns the same number. Here's my code so far:

 public class View : MonoBehaviour {
     public Tilemap tilemap;
     public List<Player> players;
     private BoardTile selectedTile;
     private model model;
 
     // add tile sprites to the tilemap
     public void setTiles() {
         Vector2Int size = model.BoardSize;
         for (int x = 0; x < size.x; x++) {
             for (int y = 0; y < size.y; y++) {
                 tilemap.SetTile(new Vector3Int(x, y, 0), model.getSprite(x, y));
             }
         }
     }
 
     // change player GameObjects' positions
     public void movePlayers() {
         foreach (Player p in players) {
             Vector2Int loc = p.CellCoordinates;
             p.GameObject.transform.position = tilemap.GetCellCenterWorld(new Vector3Int(loc.x, loc.y, 0));
             Debug.Log(x + ", " + y + ", " + tilemap.GetCellCenterWorld(new Vector3Int(loc.x, loc.y, 0))); // if there's a player on tile (0, 0) this prints 0, 0, (0.5, 0.5, 0)
         }
     }
 
     void Update() {
         // if the player presses on a tile, select it
         if (Input.GetMouseButtonDown(0)) {
             Vector3 pos = Input.mousePosition;
             Debug.Log(pos); // if i click on the center of tile (0, 0) it prints (423.2, 217.4, 0.0)
             Vector3Int cell = tilemap.WorldToCell(pos);
             selectedTile = model.TileAt(cell.x, cell.y);
             Debug.Log(cell); // this prints (423, 217, 0)
         }
     }
 }
 

So transform.position for my player GameObjects seems to be relative to the tilemap's cell coordinates, the mouse press is relative to the distance from the bottom left corner of my screen, and the methods for converting between the two (ie Tilemap.WorldToCell, Tilemap.CellToWorld, Transform.LocalPosition) aren't working the way I thought they would.

I'm sure there's something stupid I'm missing but the documentation for Tilemap and Transform and Input haven't been helpful so far and this has been stumping me for days.

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
2

Answer by rainbowbeansprout · Jan 20, 2020 at 07:32 PM

Well I feel stupid, I fixed this by replacing Input.mousePosition with Camera.main.ScreenToWorldPoint(Input.mousePosition))

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

121 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

Related Questions

How to get max and min points of a game object on the x and z axis? 3 Answers

Why transform.TransformPoint(transform.position) prints differently than single transform.position? 1 Answer

Can I use the local x,y transforms of a plane? 1 Answer

How to have an object some distance above another object Locally? 1 Answer

How to do a Overlapcirle 2d in a local Grid/Tilemap enviroment? 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