2D Physics and Pixel Perfect Movement?

Hi,

New to Unity. So far it seems absurdly easy to develop in, but I cannot for the life of me get pixel perfect movement with physics down.

Game is a top down adventure RPG (think LTTP crossed with FFVI). 8 Direction free character movement. Importantly, the game features 2x or 4x pixel graphics. If it wasn’t for that, this would all be much easier.

So far I have this (simplified):

public float speed = 1f;
private BoxCollider2D boxCollider;
private RaycastHit2D hit;

void Awake()
{
    boxCollider = GetComponent<BoxCollider2D>();
}

public void Move(float move_x,float move_y)
{
    var direction = new Vector2(move_x, move_y) * speed;
    hit = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(0, direction.y), Mathf.Abs(direction.y));
    if (hit.collider == null)
    {
        transform.Translate(0, direction.y, 0);
    }
    hit = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(direction.x, 0), Mathf.Abs(direction.x));
    if (hit.collider == null)
    {
        transform.Translate(direction.x, 0, 0);
    }
}

Player has a box collider, objects have a 2d box or polygon collider.

This elegantly solves the problem of having pixel perfect movement as each pixel is a unit. The camera translates directly. No jitter, no issues. If anyone is reading this and needs pixel perfect movement and isn’t worried about physics, this works great.

But because of the way I’m raycasting a box, if the player hits an angle, they stop and don’t bounce off of it or slide up it. It stops the player in their tracks the same as a flat wall. There aren’t any physics at play here. Pardon the pun.

e.g. I’m moving to the right and hit a slope /. Rather than the player travelling along it, northwards, the player stops in their tracks as if hitting a flat wall |. To move further, I have to move the player UP and then to the RIGHT.

If I try to use any physics (such as Rigidbody2D.velocity), without using Translate, the player doesn’t move in a pixel perfect fashion anymore. The player bounces off walls like they need to, but the whole image stutters across the screen and the player doesn’t stick to the pixel grid.

So my question is twofold:

One, is there any way to use 2D physics (a la Rigidbody2D, etc) and get pixel perfect movement?

Or, two, is there any way to modify the above to build in my own physics for bouncing off / sliding across angled walls?

Your script won’t bounce off of objects because there is no physics. As for getting this movement your options aren’t great. One thing that may work great for you is PixelBoy.cs

using UnityEngine;
using System.Collections;

namespace Nixon.Cameras.Effects
{
	//PIXELBOY BY @WTFMIG EAT A BUTT WORLD BAHAHAHAHA POOP MY PANTS
	[ExecuteInEditMode]
	[AddComponentMenu ("Image Effects/PixelBoy")]
	/*PIXELBOY BY @WTFMIG EAT A BUTT WORLD BAHAHAHAHA POOP MY PANTS
	 * "Yes that is his actual copyright"
	 * Used to pixelate the screen in a fairly cheap way */
	public class PixelBoy : MonoBehaviour
	{
		public int w = 720;
		int h;
		public Camera Camera;

		protected void Start ()
		{
			if (!SystemInfo.supportsImageEffects) {
				enabled = false;
				return;
			}
			// + + + +  Added by Louis Lombardo IV + + + +
			if (Camera == null) {
				try { 
					Camera = GetComponent<Camera> ();
				} catch (UnassignedReferenceException e) {
				}
			}
			if (Camera == null) {
				this.enabled = false;
			}
			// = = = = = = = = = = = = = = = = = = = = = =
		}

		void Update ()
		{

			float ratio = ((float)Camera.pixelHeight / (float)Camera.pixelWidth);
			h = Mathf.RoundToInt (w * ratio);

		}

		void OnRenderImage (RenderTexture source, RenderTexture destination)
		{
			source.filterMode = FilterMode.Point;
			RenderTexture buffer = RenderTexture.GetTemporary (w, h, -1);
			buffer.filterMode = FilterMode.Point;
			Graphics.Blit (source, buffer);
			Graphics.Blit (buffer, destination);
			RenderTexture.ReleaseTemporary (buffer);
		}
	}
}

This way no matter what your entire screen would be pixelated. You wouldn’t have to worry about pixel perfect movement or pixel perfect anything for that matter because it would all just be pixelated anyway.

Your other option would have to be something really, really, sketchy... something like having a separate object that you calculate physics with but only apply changes in position if it lines up with your pixel grid.

Use pixelboy.

I know this is an old tread, but I think you could achieve this by having the visual representation of the object aligned to the grid, while the actual object has a floating point transition. In other words, make the player a 2d physics object, free to move and bounce anywhere, but the sprite that the player sees is shifted to fit on the grid by rounding the position components to your unit width. This could cause pixel-floating when the character stands on the edge of a cliff, but making the collider 1 unit smaller in both directions could fix this.