Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 /
  • Help Room /
avatar image
0
Question by jaspercayne · Jan 26, 2018 at 03:26 PM · player movementcoordinates2d-gameplay

Trouble with Tilemaps

I am attempting to make a top down game using the new Tilemap system, but when it comes to trying to get the current position of the player within the map, I seem to be hitting a brick wall. I can get the correct position (Vector3Int of cell coordinates that the player is currently standing in) ONLY when I first start the scene. Once the player character moves at all, I get incorrect results every time I try to get the coordinates.

If I grab coords right after entering play mode they will be correct. If I move the player using keyboard input then attempt to grab coords again, it will report the original position of the player object every single time. What am I doing wrong here?

 private Grid parentGrid;
 private Tilemap levelMap;
 public bool updatePos = false;
 ...
 private void UpdateActorPosition()
 {
     //position = parentGrid.GetComponent<GridLayout().WorldToCell(transform.position);
     position = levelMap.layoutGrid.WorldToCell(transform.position);
     Debug.Log(position);
     updatePos = false;
 }


This is the function in question I am using to get the player position. The variables parentGrid and levelMap are set during Start() and are definitely correct when I check in the inspector, and if I move the player object around before starting the scene the coordinates will be correct when I first get them, but once again as soon as the character moves in the slightest, the coordinates become useless since they are only returning the original position the character was in when starting the scene up.

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
0
Best Answer

Answer by jaspercayne · Jan 26, 2018 at 03:37 PM

I'm sorry to do this to you guys, but I just worked out the issue:

I had the rigidbody2D and collider components on a child object to the player, and since I was adding force to the rigidbody2D through code, but not updating the parent object position as well, the player was technically staying in one location despite appearances, and all I was actually doing was sliding the rigidbody2D around the tilemap. Moving the rigidbody2D and the colliders (box and composite) to the parent object instead instantly solved the issue I was having with incorrect coordinates.

For anyone who stumbles into this in the future: Make sure you have your rigidbodies placed on the parent object to alleviate potential issues with characters being incorrectly positioned.

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

123 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

Related Questions

Rotate player based on custom axis 0 Answers

why wont my ship fire alternating guns? 0 Answers

2D depth simulation in a sidescroller 1 Answer

2D ROGUELIKE IN UNITY 5.x Level Error 0 Answers

How to make a player lose if they don't reach specific areas quick enough? 1 Answer


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