My problem is, that when Im moving against a wall, that there is a little bouncing effect. Here is a video of it: https://www.dropbox.com/s/t5drxu4aw60nq59/meem.mp4?dl=0
I dont know how to fix it, I already tried everything. I hope someone know a solution for this. Thank you Here is the movement script:
public class PlayerMovement : MonoBehaviour
{
public float speed;
public float x;
public float y;
public bool canMove;
void FixedUpdate()
{
if (canMove)
{
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
{
GoUp();
}
else if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
{
GoDown();
}
else if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
GoRight();
}
else if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
GoLeft();
}
if (Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.RightArrow))
{
GoUp();
GoRight();
}
else if (Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.LeftArrow))
{
GoUp();
GoLeft();
}
else if (Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.RightArrow))
{
GoDown();
GoRight();
}
else if (Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.LeftArrow))
{
GoDown();
GoLeft();
}
this.transform.position = new Vector2(this.transform.position.x + x, this.transform.position.y + y);
x = 0;
y = 0;
}
}
public void GoUp()
{
y = Time.deltaTime * speed;
}
public void GoDown()
{
y = Time.deltaTime * (-speed);
}
public void GoRight()
{
x = Time.deltaTime * speed;
}
public void GoLeft()
{
x = Time.deltaTime * (-speed);
}
}
Find two suggestions to handle stop going right example. Note they are going to be not debugued code, as I dont know your project details. Also no the most correct in many aspects for the saem reason. Just to give you the idea.
1.- With distance control
public GameObject wall; //ref to wall set in editor
float playerHalfSize = 0f;
float wallHalfSize = 0f;
float bodiesTotDist = 0f;
Start() {
playerHalfSize = transform.scale.x / 2;
wallHalfSize = wall.scale.x / 2; // supposing size is set with scale. Preferably get the size with a collider or the mesh renderer Bounds class inside the respective collider or MeshRenderer classes
bodiesTotDist = playerHalfSize + wallHalfSize;
}
float getDistanceToWall(wall) {
float distance = Vector3.Distance(transform.position, wall.transform.position) - bodiesTotDist;
}
public void GoRight()
{
if (getDistanceToWall() > 0) //this avoids your player x move further to the right
x = Time.deltaTime * speed;
}
If you might still see the bouncing you can try to adjust manually a threshold distance, for example getDistanceToWall() > 0.5f or adjunting the collider.
2.- With a trigger: If you put a trigger a little bit bigger that your wall, when the wall touches the player you can handle in the code also to avoid further x right set.
bool isRightWallTouching = false;
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject == wall) //for the right wall
isRightWallTouching = true;
}
void OnTriggerExit2D(Collider2D other)
{
isRightWallTouching = false;
}
public void GoRight()
{
if (isRightWallTouching > 0) //this avoids your player x move further to the right
x = Time.deltaTime * speed;
}
As told, not debugged code so might have some failures and the chance that you might have many walls, so this suggestion may lead to a lots of bools dirty solution, but hope it can provide you some inspiration to put your game to work and move on :)