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 /
avatar image
0
Question by Noshiz · Jun 17, 2018 at 06:54 PM · movementpixelworldspace

Setup Unity enviroment (world space) for Pixel Perfect movement ?

Hi everyone.

How can I setup Unity to use integers for the world space instead of floats? For example instead of the character being positioned let's say on (2.343, 4.623) and move by small increments, I want the character to be positioned on integer values (2, 4) and move by a full integer at a time.

Right now when I do that the character teleports to that coordinates because the world space uses floats and not integers so for example from position.x 0 to position.x 1 the distance is huge while I want it to be just 1 pixel. The tile size is 16x16 and I have set up the PPU at 16. Also using orographic camera with a size of 6 (because I want around 11.25 tiles vertically).

Am I missing something? Is there a setting that I'm completely oblivious to and that I can't find by searching?

Comment
Add comment · Show 2
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 Cherno · Jun 17, 2018 at 07:23 PM 1
Share

PPU need to be 1 and you also have to round the xyz values of the Vector3 from Transform.position.

avatar image Noshiz Cherno · Jun 17, 2018 at 08:31 PM 0
Share

Should I change the PPU for everything to 1? What about all those that say that the PPU should be the same as your tile size? I am aware that I also have to round the values. Thing is I've been working on the game for a while now and i only realised that I'm doing that wrong and now I have to change everything to accomodate for the PPU being 1.

1 Reply

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

Answer by SlowCircuit · Jun 18, 2018 at 02:30 AM

You have to manually adjust each object's positioning and movement. If you're moving objects using transform then you can just do this:

 void LateUpdate() {
     Vector3 pos = transform.position;
     pos.x = Mathf.Round(pos.x);
     pos.y = Mathf.Round(pos.y);
     transform.position = pos;
 }

That is also for PPU of 1 (meaning each pixel is 1 world unit). Personally, I just set everything's PPU to 1, but if you want it to be 16, then you'll have to round the position to the nearest 16 (or something like that.

If you're moving the object through rigidbodies, then it's a little more complicated.

Comment
Add comment · Show 3 · 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 Noshiz · Jun 18, 2018 at 05:40 PM 0
Share

A few questions if you don't $$anonymous$$d. First off, why in LateUpdate and not in FixedUpdate? I'm moving the object using transform so that should fit, I'm trying to stay away from moving objects using physics.

A lot of tutorials/guides etc I read always say to set the PPU to your tiles size but if setting it to 1 makes for pixel perfect movement why set it to 16 (or any other value for that matter) and have to deal with floats?

Also it's not just the tile but also the character has a width of 16 and a height of 32, meaning that if I move the character by 16 it would move by a whole tile at a time which is not desirable.

avatar image SlowCircuit Noshiz · Jun 18, 2018 at 08:45 PM 1
Share

FixedUpdate() is specifically for physics. If you're using rigidbody movement then you'd use fixed update so that all physics-related things are happening around the same moment. For moving transforms, you do not want to use FixedUpdate. You'd use Update. Using LateUpdate ensures that the position is rounded after all other changes to the value are done.

As for PPU, I personally think it's easiest/simplest just to use a PPU of 1 meaning each world unit is equal to 1 pixel. Then no matter what resolution each sprite is, you just make sure everything in the game remains rounded and you're golden.

avatar image Noshiz SlowCircuit · Jun 18, 2018 at 09:11 PM 0
Share

Ah I see. Thanks for your advice. I'll change everything to PPU 1 and use the LateUpdate to round the number. Wish I knew that before I added animation and transforms through animation, now I'll have to rework all that.

Thanks again.

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

132 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 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 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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Getting the screen size in world units 0 Answers

[Solved] Translating Camera on only X, Z axis on World Space, no matter what is the rotation 0 Answers

Converting from moving local space to world space 0 Answers

Making a bubble level (not a game but work tool) 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