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 /
  • Help Room /
This question was closed May 09, 2020 at 02:26 AM by lCira for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by lCira · May 05, 2016 at 05:51 AM · mousetilescreentoworldpointcell

Tile based mouse cursor

I want to have a "tile based" (cell) mouse cursor in my game. I have this in my script:

void Update () { Vector3 worldCoords = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector3 newPos = new Vector3((int)worldCoords.x , (int)worldCoords.y, gameObject.transform.position.z); Debug.Log(worldCoords.x + " " + worldCoords.y); gameObject.transform.position = newPos; }

But as you can see in this video. The tile "highlight" position does not correctly match the cursor's position. How can I fix it?

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

  • Sort: 
avatar image
0
Best Answer

Answer by NoseKills · May 05, 2016 at 09:05 AM

It would be easier to answer if we could see how your tiles are pivoted. However I suspect they are center pivoted and this combined with casting the mouse coordinates to int (rounding them always towards zero) is what is causing the issue. Looking at the code and video, I guess your tile size is 1x1 units...

If you have a centered tile at 0,0, its right edge at +0.5. Since you cast to int, even when your cursor is at 0.9, the tile position rounds to 0. You have to add half of the tile size to your cursor position before casting to make sure anything over .5 rounds to the next integer.

If you need this to work for negative coordinates, you should use Matf.FloorToInt() instead of casting. Otherwise e.g. anything from -0.9 to +0.9 will round to 0, and you will get a bug.

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 lCira · May 06, 2016 at 08:10 AM 0
Share

I used Tiled to make the map, and my tiles are 16x16, so 16 pixels per unit, and when I put the map into the scene its pivot is in the top left (I don't know how to change the pivot of the whole map). While reading your answer, I realized that the pivot of the highlight is in the center so I changed it to top left, same as the map, and now it works normally. Thanks for the reply! Thanks also for your tip about negative coordinates :)

avatar image lCira lCira · May 06, 2016 at 08:20 AM 0
Share

I just tried $$anonymous$$athf.FloorToInt() and it's not working correctly. I'll try to fix it. Edit: Fixed it by checking if worldCoordinate is less than 0 or not then do the thing

Follow this Question

Answers Answers and Comments

57 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

Related Questions

Check the if mouse clicked on specific tile 0 Answers

Move object to mouse position 1 Answer

How To Get Tile Cell Position 0 Answers

ScreenToWorldPoint works wrong after updating to 2019.3.5 1 Answer

Render mouse in VR? 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