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 Guppie1337 · Oct 30, 2015 at 04:18 PM · editormathflevel editorcalculations

Mathf.Floor/Editor Common Practices

I'm creating my own level editor and I'm drawing lines to create a grid for a TileMap. I'm having trouble understanding some common practices I've seen in comparison to the way I am currently using Mathf.Floor.

public float height = 32f;

My code:

 for (float x = 200; x < 400; x += height)
 {
     Mathf.Floor(x);
 }
 
 //First 3 results:
 // 200
 // 232
 // 264

Other code:

 for (float x = 200; x < 400; x += height)
  {
      Mathf.Floor(x/height) * height;
  }
      
  //First 3 results:
  // 192
  // 224
  // 256

I've noticed that either way in solving this issue, the increment is still going to be 32 units between each result. The resulting grid at the intersects will still produce "perfect" 32x32 squares within the grid. To me, it seems that the resulting values are arbitrary and the only factor necessary to have is the proper increment.

Why is it that dividing and multiplying by height is so common as seen in the second method when using Mathf.Floor?

Comment
Add comment · Show 4
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 starikcetin · Oct 30, 2015 at 04:49 PM 0
Share

Why you do flooring in first example? All the outputs would be integer already.

avatar image Guppie1337 starikcetin · Oct 30, 2015 at 06:05 PM 0
Share

These were just values I decided to plug in for the explanation. I could have added any float but chose not to for the sake of easy reading.

avatar image maccabbe · Oct 30, 2015 at 06:19 PM 0
Share

$$anonymous$$athf.Floor(x/height) * height;is a simple way to align the the grid to 0 and force the value to be a multiple of height

0, 32, 64, 96, 128, 160, 192, 224, 256

while $$anonymous$$athf.Floor(x); does not align the grid to 0

8, 40, 72, 104, 136, 168, 200, 232, 264

and also does not force the value to be a multiple of height

avatar image Guppie1337 maccabbe · Oct 30, 2015 at 06:52 PM 0
Share

This is exactly the explanation I was looking for. In my examples where I tried to convince myself they were the same thing I would never include 0. I completely left out the idea of alignment to 0. Thanks for the help.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bonfire-Boy · Oct 30, 2015 at 04:58 PM

The second version ensures that the number you come up with is an integer multiple of height; specifically, the integer multiple of height that's closest to x and less than x.

I'm not convinced that it is all that common to be honest. It would account for a tiny fraction of the many uses I've put Floor operations to over the years. But here's a use case...

Say you have a number of equally spaced horizontal floors, with dist being the distance between them.

You could then use Mathf.Floor( yPos/dist ) * dist to find the height of the floor that's below an arbitrary object (the height of the object being yPos). The floor, in other words, that the object would hit first it if fell down.

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 Guppie1337 · Oct 30, 2015 at 06:59 PM 0
Share

In the scenario you provided, why would one want to use $$anonymous$$athf.Floor if the absolute distance was to be expected? e.g. Something is 10.8ft away you wouldn't say it's only 10ft away.

avatar image Bonfire-Boy Guppie1337 · Oct 31, 2015 at 12:20 PM 0
Share

I'm sorry, I don't understand your question. I'm talking about finding the height of the floor that an object would fall to. So for example (using your numbers) if the object is at 10.8ft and the floors are at heights of 10, 20, 30ft etc, then the formula gives you 10ft.

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

34 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

Related Questions

Assigning multiple assets to multiple variables on inspector 1 Answer

Camera Preview is empty 5 Answers

Assets folder not loading 1 Answer

Unity editor crashing on Intel graphics on Macbook Pro 0 Answers

really weird unity behavior please HELP???? 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