Hello, i have a player object which can move left and right and jump, there is a box collider2D on the player, as well as the ground i have in my test scene. The box colliders are matched to their sprites, but for some odd reason the player object appears to stop ever so short of hitting the wall, however the other wall it appears normal.
If you’re 100% certain all of your sprites and colliders are matched up, another possibility is that the camera is slightly rotated on the Z axis or one of your sprites is slightly rotated on the Z axis.
Double check your sprites and colliders. Double check the rotation of all your objects including your camera.
How odd, double checked all of the involved objects and they are not rotated on the Z axis at all.
Here’s some screenshots of the objects, one with just wireframes so the boxes are easier to see.
Whats the yellow gizmo that’s offset from the square? It doesn’t look aligned to the collider.
How are you moving the player?
@PraetorBlue It’s a boxcast, drawn with OnDrawGizmos to help me see where it is. It’s placed on top of a transform, but those do not have any collision, to my knowledge. Here’s the code:
private void OnDrawGizmos()
{
Gizmos.color = (Color.blue);
Gizmos.DrawWireCube(GroundCheck.position, bc.bounds.size);
Gizmos.color = (Color.yellow);
Gizmos.DrawWireCube(WallCheck.position, bc.bounds.size);
}
bool IsGrounded()
{
RaycastHit2D casthit = Physics2D.BoxCast(GroundCheck.position, bc.bounds.size, 0f, Vector2.down, 0f, GroundLayerMask);
return casthit;
}
bool CanWallJump()
{
RaycastHit2D casthit = Physics2D.BoxCast(WallCheck.position, bc.bounds.size, 0f, Vector2.right, 0f, GroundLayerMask);
return casthit;
}
And @Madgvox This would be that code:
void Update()
{
PlayerMove();
PlayerJump();
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
}
void PlayerMove()
{
if (Input.GetAxisRaw("Horizontal") != 0)
{
transform.localScale = new Vector3(Input.GetAxisRaw("Horizontal"), 1, 1);
Vector2 velocityX;
velocityX = new Vector2(Input.GetAxisRaw("Horizontal") * GroundedMoveSpeed, 0);
rb.velocity = new Vector2(velocityX.x, rb.velocity.y);
}
else
{
rb.velocity = new Vector2(0, rb.velocity.y);
}
}
void PlayerJump()
{
if(IsGrounded() || CanWallJump() )
{
if (Input.GetKeyDown(KeyCode.Space)){
rb.AddForce(new Vector2(0, 5), ForceMode2D.Impulse);
}
}
}
Apologies if it’s a bit messy looking.
How odd that it only seems to behave this way when moving itself to a wall on the right. Left looks like i would expect it to.
A BoxCast itself won’t cause any forces. However I note that to perform your boxcast (and draw the gizmo) you’re referencing something called WallCheck
. What kind of variable is that, and what is it referencing? Do you have a collider on a child object to facilitate the positioning of the boxcast maybe?
Both “GroundCheck” and “WallCheck” are just transforms. Child objects of the player object. I could have just cast the boxcasts from the player object itself but i figured it would save a lot of trouble to just use transforms. They don’t have colliders.
bump
How bizarre, in the scene view it appears to be this way in both directions, but in game view, only when going right. (You may need to zoom the image in to see it)
Is your camera orthographic or perspective?
Orthographic, though it has the pixel perfect component added to it.
This reallllly looks to me like there is a z-axis difference between your player sprite and the obstacle sprites, and a slight Y rotation on the ortho camera. But you said you checked that already so…
is your scene camera perfectly head-on (using one of the 6 axis control buttons in the top right of scene view)?
how do i check this?
The pixel perfect component would do it. If your colliders aren’t aligned on pixel boundaries, you may get a perceived difference between the collider position and the rendered position.
Especially if the pivot of your graphics is centered rather than locked to a pixel boundary like the lower left, for example.
If it’s aligned properly it will say one of the six axial directions: back, front, right, left, etc… For example:
If it’s slightly off it will just say “persp” or “iso” depeding on whether it’s set to perspective or isometric.
You can click on those cones coming out of that central cube to switch between the 6 on-axis camera views, and you can click on the little “iso” or “persp” label to switch between those two settings. You can also lock or unlock the camera view with the padlock icon in the top right.
You should double check that your scene view camera is aligned like that, and also double check that your in-game camera’s rotation is some multiple of 90 for each of the euler axes in its inspector (but you said you checked this above I believe).
While messing around with possible ideas, i tried setting the scale of both walls to 3-8-1, which caused something interesting, you can now see it when going both ways, not just right.
The more i use it, the more this engine confuses me. Geez.
Also, i don’t see that gizmo in the corner of the screen. Would that be because this is a 2D project?
Oh my gosh, i solved it (I think). The pixel perfect camera doesnt seem to like floats. I set the positions and scales of everything in the scene to a whole number, no decimals or anything, and the issue is entirely gone.