AuthorTopic: How to Handle Instance Animation on Moving Charackter? (Solved)  (Read 6334 times)

Offline appo

  • 0001
  • *
  • Posts: 10
  • Karma: +0/-0
    • View Profile
Is this any help?

https://nielson.io/2015/10/fixing-gaps-between-sprites-better-2d-in-unity-part-2
not really. thats a other topic which covers kinda the same area but not exactly.
someone made a video in german
https://www.youtube.com/watch?v=nkzmbgyjblM
which  covers and solve the issue but before he did it i moved away from unity and i give GameMaker Studio2 a try. No need to watch a 24 minute video for simple stuff like pixel snapping to Allign objects witch each other..its just works there with the help of the grid system. Unity and Pixel Art seems to be kinda, possible but its tedious from what i read. I sat there days just to solve how to place two pixel graphic images (gun and the body) pixelperfect with each other while it was a thing of seconds in gms2 like in any simple graphic programm. due to that early frustating situation i switched.

Offline daramon

  • 0001
  • *
  • Posts: 73
  • Karma: +0/-0
    • View Profile
I can see that. I'm literally heavily invested in Unity though, so I went round the houses looking for solutions to these kinds of seemingly simple issues.

In case you come back to Unity, or in case anyone else is looking for solutions to the elusive pixel snapping issue, here's my incredibly simple take.

1: Create a shader file and copy-paste this in using your code editor:
https://gist.github.com/talecrafter/81e2f3bb7fb4b4fc367e7b851772b646

2: Create a material file and apply the shader using the selector in the inspector. Set your "Pixels Per Unit" in here as well. (I use 1 for simplicity and it's great.)

3: Apply to any SpriteRenderer components (attached to any game objects) you want to pixel snap. This is pretty crazy. If you zoom in the editor, you'll see game objects snapping around when you try to move them by sub-pixel values.

5: Now for the camera. It doesn't use a sprite renderer and applying a shader to the whole screen is not a great idea. So the answer is simple: create a script for it and copy-paste this in:

Code: [Select]
    public class PixelSnap : MonoBehaviour
    {
        private Vector3 _tempPosition;

        private void LateUpdate()
        {
            _tempPosition = transform.position;
            transform.position =
                new Vector3(
                    Mathf.RoundToInt(transform.position.x),
                    Mathf.RoundToInt(transform.position.y),
                    transform.position.z);
        }

        private void OnRenderObject()
        {
            transform.position = _tempPosition;
        }
    }

This assumes a "Pixels Per Unit" of 1, but the maths is pretty trivial to update even if you're running at 16 or 32. (Feel free to shout if you need help.)

And that's it. Camera movements snap to pixels. If you want a pixel art game with non-pixel-art UI, just don't apply the pixel snap material to your UI components. If you want game objects to move together, put one as a parent of the other apparently (untested in my game.)

What this doesn't do is limit the screen to old console game dimensions, force game pixels to be multiples of screen pixels and introduce borders or black bars. So I probably have some issues ahead of me still...

(Admin/mods, if you want me to move this to general, just shout.)

I will have a look at Gamemaker Studio and that video, but I don't speak German! ;)