Hello
I wanted to make a platform game. I made a simple script for walking (like very simple) to check if everything is alright. Well it’s not. If I go straight to a wall, I go inside the wall and then it’s pushing me back. I colide with it but it’s not exactly what I want.
That’s a part of my script for walking (It’s working just fine):
void Update()
{
if (Input.GetKey(KeyCode.RightArrow))
transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
//then there’s code for going left, down and up.
}
I set the colliders to walls and the character. I also set the character to be rigidbody.
What’s wrong, why is it bugging?
It’s happening because you are moving the Rigidbody2D by moving the Transform i.e. just warping the body from position to position (think teleportation without actual movement). You then move (teleport) it so it overlaps a collider (the wall) and the physics system solves the overlap by moving it away.
Either you want the wall to stop the player collider or you don’t. If you don’t then set the player collider to be kinematic.
If you want it to collide with the walls then leave it as dynamic (not kinematic) and move it via the Rigidbody2D velocity either by adding forces, set the velocity explicitly or using Rigidbody2D.MovePosition. Note that the simulation only updates during fixed-updates so doing this work in the Update (per-frame) callback still means it won’t actually respond until the next fixed-update.