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?