Helpful tips in creating functunalities

Here are some tips in how to create a new functionality, according to me: If you have something that is not so simple, write pseudocode...

Saturday, January 26, 2019

Pixel Perfect Movement for your 2D game's tile problem

I have had a problem where my 2D game's tile "gaps" kept on increasing and decreasing in size. Here it is in the video:

Here is the article that has solved the problem: Setting up Unity for pixel-perfect 2d project .

Here is the code that I've attached to my character (the camera is a child GO of it):

void Update()
    {
        Vector3 roundPos = new Vector3(RoundToNearestPixel(transform.position.x, Camera.main), RoundToNearestPixel(transform.position.y, Camera.main), transform.position.z);
        transform.position = roundPos;
    }
    public static float RoundToNearestPixel(float unityUnits, Camera viewingCamera)
    {
        float valueInPixels = (Screen.height / (viewingCamera.orthographicSize * 2)) * unityUnits;
        valueInPixels = Mathf.Round(valueInPixels);
        float adjustedUnityUnits = valueInPixels / (Screen.height / (viewingCamera.orthographicSize * 2));
        return adjustedUnityUnits;
    }

No comments:

Post a Comment

The Art of Game Development

The Art of Game Development
A Guide for Beginners (eBook)