Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 alexanderflink · Jul 31, 2014 at 10:24 PM · 2dmovementgridsnap

Check if object is on grid

Hello, my game is made up of a 0.64x0.64 unit grid of blocks. The player can move sideways, but the movement is limited to these grid locations (if you press right, the character moves right until it hits the next grid spot). This is my current implementation:

 var horizontalInput: float;
 var targetPosition: Vector3;
 private var moveSpeed: float = 1f;
 function Start () {
     horizontalInput = Input.GetAxis("Horizontal");
     targetPosition = transform.position;
 }
 
 function Update () {
     if (transform.position.x == 0f || transform.position.x == 0.64f  || transform.position.x == 1.28f || transform.position.x == 1.92f || transform.position.x == 2.56f || transform.position.x == 3.2f || transform.position.x == 3.84f) {
         if (Input.GetButton("Horizontal")) {
             targetPosition.x = transform.position.x+(0.64f*Input.GetAxis("Horizontal"));
         }
     }
     

     transform.position = Vector3.MoveTowards(transform.position,targetPosition,moveSpeed*Time.deltaTime);
 }

However, As you can see, the if statement that evaluates wheter the character is on a grid position is awful. How do I go about finding out if the character is on a grid spot in a nice manner?

Thanks!

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 VesuvianPrime · Aug 01, 2014 at 01:51 AM 0
Share

As a suggestion: please define your grid dimensions as a const somewhere

public const float CELL_WIDTH = 0.64f;

Also, keep in $$anonymous$$d that float comparison is difficult to get right. Due to rounding errors you will need to compare within some epsilon

Floating point comparison

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Aug 01, 2014 at 02:25 AM

The easiest solution based on the code you have here is to replace your if() on line 10 with:

  if (transform.position == targetPosition) {

Note that the comparison between Vector3s has some 'slop' so that direct comparisons works. It does not suffer from the same problem as directly comparing floating point numbers as outlined by @VesuvianPrime.

If you wanted to only compare the 'x' value as you've done in your code, you could do:

  if (Mathf.Abs(targetPosition.x - transofrm.position.x) < someSmallValue) {

A note about your code not related to your question: when creating grids like this, if at all possible use Integer values rather than values like 0.64f. You can always move the camera and/or change the OrthographicSize and/or change the FieldOfView to get a fit for an integer layout. And so many things are easier to code with integers.

Comment
Add comment · Show 1 · 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 alexanderflink · Aug 01, 2014 at 07:10 AM 0
Share

Thank you, this should work wonders. Regarding your note on the grid scale, the reason i've done this way is because my blocks are 64x64 pixels. $$anonymous$$any people recommended against setting the pixel to unit scale to 1, since the blocks would eventually come really far away from the 3d origin, which apparently causes problems. A block would be 64x64 units, and that's not really practical, is it? Do you think having a really tall level of 64x64 blocks is okay, or will it cause problems?

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

23 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

Related Questions

Drag Rigidbody into slots? 2 Answers

Creating a 2D Movement Grid 0 Answers

Grid Movement VS Free Movement 1 Answer

How Can I Create a Button that Takes Multiple Objects and cycles them through a grid? (Unity 2D) 0 Answers

Make movement vertically,How to move Vertically? 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