The problem is simple: I have a player with a rigidbody 2d and a collider 2d attached to it and I have a wall with a collider 2d attached to it. When I move the player against the wall they do collide but my player starts to shake really fast and it looks like it wants to pass through the wall.
I need the player to stop moving when it collides with a wall and to deny any further movement towards the wall without disrupting movement away from the wall.
Player movement code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
// Variables
public bool moving = true;
float playerMovSpeed = 5.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (moving == true)
{
PlayerMove();
}
PlayerMoveCheck();
}
public void Set_moving(bool val)
{
moving = val;
}
void PlayerMove()
{
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.up * playerMovSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector3.left * playerMovSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.down * playerMovSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector3.right * playerMovSpeed * Time.deltaTime, Space.World);
}
}
void PlayerMoveCheck()
{
if (Input.GetKey(KeyCode.W) != true && Input.GetKey(KeyCode.A) != true && Input.GetKey(KeyCode.S) != true && Input.GetKey(KeyCode.D) != true)
{
moving = false;
}
else
{
moving = true;
}
}
Thanks in advance.