- Home /
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?
PPU need to be 1 and you also have to round the xyz values of the Vector3 from Transform.position.
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.
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.
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.
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.
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.