So I started making a 2D game in unity that is essentially set up on a 4x7 grid (150 pixels per unit). I have a square sprite (1x1) that has both a Rigidbody 2D and a Box Collider 2D. The walls (also 1x1) have just a Box Collider 2D. Both box colliders are left at size 1 for X and Y. The RigidBody has gravity and angular drag set to 0 and fixed angle checked. All of the other properties on all of the physics components were left as their default values.
I’ve attacked a PlayerController script to my player. The script(C#) for player movement is simple so far:
public class PlayerController : MonoBehaviour {
public float speed;
void FixedUpdate() {
//&& isFree(-Vector2.right)
if (Input.GetKeyDown(KeyCode.LeftArrow))
rigidbody2D.velocity = new Vector2(-speed, 0);
if (Input.GetKeyDown(KeyCode.RightArrow))
rigidbody2D.velocity = new Vector2(speed, 0);
if (Input.GetKeyDown (KeyCode.UpArrow))
rigidbody2D.velocity = new Vector2(0, speed);
if (Input.GetKeyDown(KeyCode.DownArrow))
rigidbody2D.velocity = new Vector2(0, -speed);
}
}
All of the walls are at whole x and y locations such, like (0, 3, 0). If the players starts at (0,0,0) and moves up to the box, it stops a tiny bit before the walls edge. If the edge is at (0,3,0), the player edge would stop at (0,2.98,0).
Here is an image of the behavior. The green thing is the player and the dark brown box is the wall. The light brown is just the background. The box colliders of the wall and the player are not touching, even though the player already stopped.
If I wanted to get pixel perfect collisions, namely the player moving up to the exact edge of the wall, is my script adequate. Is there some project physics setting that is causing this? Are the Box Colliders supposed to be touching when the player stops?
NOTE: An interesting note is that when I tested this same scenario with 3D objects, the cube player moved all the way up to the wall.
Any help would be greatly appreciated! It’s also very late here so if this doesn’t make sense or I can provide some more information to help, please let me know.
-Alec
