Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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
3
Question by GTHaxor · Jan 23, 2012 at 04:41 AM · mousegridisometric

Mouse position to Isometric Grid Tile number

I am trying to use a mouse position to find a grid location (column / row). Can anyone help me write a few lines of code to make this happen?

The grid starts at world position (0,0) and stretches downwards and out. The maximum number of rows is 61 and the maximum number of columns is also 61. The entire grid is stored in a single dimension array. Indexes 0 through 60 represent row 0, columns 0 - 60. Index 61 would mean row 1, column 0. See photo here: Image

The tiles are 4 units in height and 8 units in width.

Comment
Add comment · Show 1
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 Fattie · Jan 23, 2012 at 10:42 AM 0
Share

By the way ... there's no resin you can't do this with colliders!

Just shoot a ray downwards, and find out which square it is.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jan 23, 2012 at 05:42 AM

Is there any good reason why you arranged your squares at 45°? You should just arrange them paralell to the world axes and place the camera rotated at the desired position.

The coordinates are just the world position divided by the tile size. One tile is 4x4 so if you use the Plane class for example to get the world position of your click, just divide it by 4 and "round down" / "truncate the number" / "cast to int" to get the index value.

You should really rephrase your question since this is a Q&A site, not a free-script-shop.

Here's a small code snippet how to get the world position of the mouse on a Plane:

//C# // Define your ground plane by create an Plane instance with your plane coordinates / normal Plane myPlane;

Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); float distance; if (myPlane.Raycast(ray,out distance)) { Vector3 pos = ray.GetPoint(distance); int xIndex = (int)(pos.x / 4); int yIndex = (int)(pos.y / 4); int index = yIndex*61 + xIndex; // Do something }

You had negative numbers in your image so keep in mind that you might need to invert one number first.

If your ground plane goes through (0,0,0) and is facing upwards just create the plane like this:

Plane myPlane = new Plane(Vector.up,Vector.zero);

The first parameter is the normal vector, the second a point on the plane.

Comment
Add comment · Show 5 · 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 GTHaxor · Jan 23, 2012 at 05:47 AM 0
Share

Thanks for the reply. The tiles are not squares at all; ins$$anonymous$$d, they are diamonds that sit on a plane.

avatar image syclamoth · Jan 23, 2012 at 05:54 AM 0
Share

Do the diamonds have four right-angled corners, with each side being the same length as all the others? Because that sounds awfully similar to a grid of squares rotated to 45 degrees.

avatar image GTHaxor · Jan 23, 2012 at 05:57 AM 0
Share

Since I'm apparently a complete idiot and have no idea what a square is, I searched google for what I am referring to. http://i.imgur.com/mh$$anonymous$$rX.png

avatar image syclamoth · Jan 23, 2012 at 06:03 AM 1
Share

It's a square, transformed into 3-dimensional space, and then rendered onto a 2-dimensional plane at 45 degrees. What's your point?

avatar image Eric5h5 · Jan 23, 2012 at 07:27 AM 0
Share

Right, there's no reason to make things hard on yourself. Just use squares like syclamoth said. It would also be generally simpler not to use negative numbers for coords.

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

7 People are following this question.

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

Related Questions

mouse position percentage problem 0 Answers

Help with turn based Movement in a grid 2 Answers

Gameobject follow mouse on center 1 Answer

Moving an object on the X axis corresponding to mouse movment 0 Answers

Accurate Mouse Snap to Grid within Threshold? (Float Rounding Errors) 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