I have a tile map for my game, but my Player uses a sprite, since a tile map position is store in Vector3 Int space and I want my 2D player to move in floating point precision.
The problem however is the scaling. It seems the tile map is scaled as power of 2 and the sprite is not.
Because of this a pixel on my tile map is scaled to 6 pixels in the tile map consistently. But on my player
sprite it is not scale the same way where the pixel size is scaled to 5 or 6 pixels. How can I have the two match up when scaled?
UPDATE: Just noticed that both has this scaling problem. Is there a way to have it scale by a power of 2?
You don’t want the adjust the scale at all (I’m assuming you mean Scale on the Transform), and instead draw your artwork at the scale you want. Ensure that the Pixels Per Unit (a setting on the Texture for your sprites) is the same for both.
All your sprite and tiles will be scaled to their pixels per unit when they are imported. Generally a tilemap is set to 1 tile per unit, so you want your tile graphic import settings to match up neatly with their pixle count. So if you’re using 64x64 tiles, you should have the pixelsperunit set to 64. Likewise your player sprites need to be set appropriately for that as well. For 2D you’d generally would want everything to be using the same pixels per unit, but there are reasons to sometimes change it. For example, lets say in the above scenario my player sprites are 128x128. If I wanted my player to only fill 1 tile though, I’d set their pixelsperunit to 128. If I wanted the player to fill 4 tiles, I’d set it to match the tiles at 64. Alternately if you have a player graphic that is 32x32, again if I wanted it to fill the full tile, I’d set the pixels per unit to 32, but if I wanted it to have its actual relative resolution at 1/4 of a tile then I’d use 64.
Thanks for the reply guys I am using the same PPU for all tiles (player included), the thing is, is it possible to be scaled up at a fixed power of 2, I am not scaling the transform I mean the rendered tile 64 x 64 doesn’t scale up correctly in different resolutions by the renderer. The way it is rendered to the screen. A single pixel is scaled up by either 5 or 6 pixels (5x5, 6x6 so a 1:5 or 1:6 ratio) on my 2k monitor. I’d like the renderer to scale it up uniformly by let’s say by a power of 2, since all pixels will then be scaled (in different resolutions) to let’s say a 1:6 ratio
Edit: is there a setting I could do in main camera or perhaps in another place like resize algorithm, since my tile map is sliced, clamped, tight, point (no filter), no compression
No, you’re right! I installed pixel perfect camera and tried it out but the options didn’t seem to match what I wanted. I might be using it wrong though. Will have a look at the tutorial you posted as well.
your understanding of why it is scaling is wrong, the amount of pixels you are seeing in different resolutions is because the camera is more “zoomed out” ( has more visible space) in different resolutions
No, I understand why it is scaling wrong. you’ll always have to sacrifice somewhere when scaling, but what I am proposing is that there may be a way to either, not scale or scale at a consistent ratio. But I don’t know unity that well which is why I am asking
This is what I use currently and works well. The pixel-perfect package takes care of this for you and then some.
PixelPerfectCamera.cs
using Systems;
using UnityEngine;
// ReSharper disable once CheckNamespace
public class PixelPerfectCamera : MonoBehaviour
{
public int ppuScale = 2;
public int pixelsPerUnit = 32;
public bool killCamScript = true;
// Use this for initialization
void Start()
{
//Resize the camera for PP viewing (use the old method in the editor b/c Unity doesn't seem to want to switch screen res in editor)
ResizeByCameraOrthoSize();
//Done, kill it
if (killCamScript)
DestroyImmediate(this);
}
void ResizeByCameraOrthoSize()
{
//Old method - will not have the benefit of a lower screen resolution
double orthoSize = 1.0 / (2f / Screen.height * pixelsPerUnit);
orthoSize = orthoSize / ppuScale;
// ReSharper disable once PossibleNullReferenceException
Camera.main.orthographicSize = (float)orthoSize;
}
}