2D Colliders don't actually touch, causing problems
I tried asking this question before, but I guess the question never got posted for some reason. I have this script I made so the character moves on a grid-based system. To make the transition between the grid space seemless, each grid is 8x8 pxiels long/wide, and so each tile (still 8x8) on the tilemap is going to be 1 unit. 1/8 = 0.125 so the character moves 0.125 per update until they reach a whole number. Here is the code for it:
private Rigidbody2D con;
public bool up;
public bool down;
public bool left;
public bool right;
public bool inputer;
private Vector2 rict;
public bool upc;
public bool downc;
public bool leftc;
public bool rightc;
void Start () {
con = GetComponent<Rigidbody2D> ();
con.MovePosition(new Vector2(Mathf.Round(con.position.x), Mathf.Round(con.position.y)));
}
void FixedUpdate () {
if(right || left || up || down) {inputer = false;}else{inputer = true;}
if(inputer && Input.GetAxisRaw ("Horizontal") > 0 && !rightc){leftc = false;downc= false;upc= false; right = true;rict = new Vector2 (0.125f, 0);con.MovePosition(con.position + rict);}//rict = new Vector2 (0.25f, 0);
else if(inputer && Input.GetAxisRaw ("Horizontal") < 0 && !leftc){rightc = false;downc= false;upc= false; left = true;rict = new Vector2 (-0.125f, 0);con.MovePosition(con.position + rict);}//rict = new Vector2 (-0.25f, 0);
else if(inputer && Input.GetAxisRaw ("Vertical") > 0 && !upc){downc= false;leftc = false;rightc = false; up = true;rict = new Vector2 (0, 0.125f);con.MovePosition(con.position + rict);}//rict = new Vector2 (0, 0.25f);
else if(inputer && Input.GetAxisRaw ("Vertical") < 0 && !downc){upc= false;leftc = false;rightc = false; down = true;rict = new Vector2 (0, -0.125f);con.MovePosition(con.position + rict);}//rict = new Vector2 (0, -0.25f);
else if (right && con.position.x < Mathf.Ceil(con.position.x)){con.MovePosition(con.position + rict);}
else if (left && con.position.x > Mathf.Floor(con.position.x)){con.MovePosition(con.position + rict);}
else if (up && con.position.y < Mathf.Ceil(con.position.y)){con.MovePosition(con.position + rict);}
else if (down && con.position.y > Mathf.Floor(con.position.y)){con.MovePosition(con.position + rict);}
else if (right) {right = false;}
else if (left) {left = false;}
else if (up) {up = false;}
else if (down) {down = false;}
else {rict = new Vector2();}
if(con.position.x % 0.125 != 0){con.MovePosition(new Vector2(Mathf.Round(con.position.x), con.position.y)); if (right) {right = false; rightc = true;} else if (left) {left = false; leftc = true;} else if (up) {up = false; upc = true;} else if (down) {down = false; downc = true;}}
if(con.position.y % 0.125 != 0){con.MovePosition(new Vector2(con.position.x, Mathf.Round(con.position.y)));if (right) {right = false; rightc = true;} else if (left) {left = false; leftc = true;} else if (up) {up = false; upc = true;} else if (down) {down = false; downc = true;}}
}
}
As you can see, the if statement at the end is for collision detection, if the character hits a wall or some kind of collider, it will round back to the previous position. This is my temporary solution to another problem I was having, and I don't like how this one works. I had the character's box collider pixel perfect, it was the right size and everything, because of that if the player hit a wall, it wouldn't actually stop the player but softlock the player. Their movement wouldn't register because, in Unity, collision detection doesn't actually mean touching if the player gets too close to a wall (a fraction of a fraction) they player will actually not touch the collider, this seems to be just how the engine works. so if the player is not exactly moving properly, and the collider moves the character a fraction of a fraction, then that character will never truly be a whole number as a result. To circumvent this I made the player's collision box slightly smaller so that when they are standing next to a wall they don't get pushed off the grid layout by a slight (and unnoticeable) amount which would softlock the player. So what's the problem I'm still having? Because the player's 2d collider box is smaller than they are supposed to if they hit a wall they will be jolted back to the previous grid tile. This jolt animation is something I don't want. I have YT videos showing what I mean, but I don't think I can post them here, because last time I posted a question with a video of what I needed help with, the post never got posted due to a link. So hopefully this worded description is enough to get you understanding. If you need more description ill gladly try my best.
Your answer
Follow this Question
Related Questions
All colliders not longer working 0 Answers
How to prevent a 2D Kinematic asset passing through a 2D Box Collider. 1 Answer
No overload for method. (Question) 0 Answers
Do I need Rigid2DBody if I don't plan to use physics? 2 Answers
Can someone explain how this collision detection work and why it fails sometimes? 0 Answers