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 /
avatar image
0
Question by Cheeseless · Mar 14, 2017 at 08:48 AM · 2dgridpositioning

Locate a free-moving 2D object's position in a grid.

Ok, so I have a player object that can move freely in all directions, with no snapping to the above mentioned grid. I need to figure out in which square of the grid that object is.

The grid is not a game object itself currently, it's only meant to give a rough position of the player (GridX, GridY) for some other objects to use. It starts as (0,0) in the lower left corner of the screen (this is a static level) and increases in both axis across the screen. The grid divisions are square, and the size of each square is a fixed number of Unity units (I don't know how big it should be yet, sprites aren't in). The grid is also always only 10x4, and the player can only move within it (that part's easy enough, though).

I can't just do this with a big stack of ifs, there must be a better way.

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

Answer by ShadyProductions · Mar 15, 2017 at 01:43 PM

 public Vector2 GetGridByPosition(float x, float y) {
 return new Vector2(Convert.ToInt32(x),Convert.ToInt32(y))
 }

This will return your Vector2 position of the grid. It should be pretty accurate. It rounds the given position to the nearest int.

Depending if your grids go as follows: 1,1 1,2 1,3 etc

Else, you'll have to adapt it to your grid size

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 Cheeseless · Mar 15, 2017 at 04:23 PM 0
Share

Oh, I'm so dumb. Thank you! This makes perfect sense.

avatar image Cheeseless · Mar 15, 2017 at 04:25 PM 0
Share

I need to slightly adapt that if the grid is bigger than one Unity unit, but it gave away the logic I should use.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Need to make grid movement more smooth 1 Answer

Snap object to grid made from cubes during runtime 0 Answers

Unity2D, place GameObject at the top of another GameObject 0 Answers

Changing the placement of circular motion in 2D? 1 Answer

How can I make the character jump between the boxes? 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