Hello readers, I have a character with a rigidbody and a script(below). It allows the player to move but when it collides with a wall while holding down the input, the character starts to vibrate and gets partially absorbed into the wall. The character will then be pushed in the opposite direction. Any idea what might be the cause?
Here is the code:
[Range(1, 100)]
public float MoveSpeed = 10f;
bool left;
bool right;
Rigidbody rb;
void Awake () {
rb = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
if (Input.GetAxis("player_move_joy") < 0 || Input.GetAxis("player_move") < 0) {
left = true;
}
if (Input.GetAxis("player_move_joy") > 0 || Input.GetAxis("player_move") > 0) {
right = true;
}
}
void FixedUpdate(){
if (left)
{
rb.MovePosition(transform.position + Vector3.left * MoveSpeed * Time.deltaTime);
left = false;
}
if (right)
{
rb.MovePosition(transform.position + Vector3.right * MoveSpeed * Time.deltaTime);
right = false;
}
}
}