Hello, this weekend i participated in the GMTK Game Jam 2021 and got at weird “bug” i would like to have fixed in future projects. When the wall is for example to the right and the player goes to the right until they´re stopped by the wall, the player bumps kinda into the wall and there´s a weird screen shake because of the cinemachine camera that follows the player. You can play the game and see the issue by yourself here: Square Pair by CodingCrate
It feels like you are moving the square with .Translate() or .position + = moveVector which is not very correct. If your player object has a Rigidbody or Rigidbody2D component, then there are several ways to move the player:
First, we get the Rigidbody2D component:
Rigidbody2D rb2D = GetComponent<Rigidbody2D>();
-
With the
.velocitydestination:rb2D.velocity = moveVector;
But you should be very careful when changing the velocity. You have to take into account that you need to keep the velocity.y value in order for the player to fall correctly. -
By adding force (
.AddForce()):rb2D.AddForce(moveVector);
But here, too, you should be careful. In this case, the player will be able to accelerate infinitely, so you need to limit the horizontal speed:// declare and assign variable |maxSpeed|
var xSpeed = rb2D.velocity.x;
xSpeed = Mathf.Clamp(xSpeed, -maxSpeed, maxSpeed);
rb2D.velocity = new Vector2(xSpeed, rb2D.velocity.y); -
With
.MovePosition():rb2D.MovePosition(transform.position + moveVector);
This method is probably more suitable for you than others.