I'm making a game in unity 2d and when my player goes in to a wall he gets stuck and can't move at all. Here is a video:
I've tried using a composite collider, physics material with friction at 0.
Here is my movement script:
public class PlayerMovement : MonoBehaviour
{
Vector3 pos;
float speed = 2.0f;
private Animator animator;
void Start()
{
pos = transform.position;
animator = gameObject.GetComponent<Animator>();
}
void FixedUpdate()
{
if (Input.GetKey(KeyCode.W) && transform.position == pos)
{ // Up
animator.SetInteger("isWalking", 1);
pos += Vector3.up;
}
if (Input.GetKey(KeyCode.S) && transform.position == pos)
{ // Down
animator.SetInteger("isWalking", 2);
pos += Vector3.down;
}
if (Input.GetKey(KeyCode.D) && transform.position == pos)
{ // Right
animator.SetInteger("isWalking", 3);
pos += Vector3.right;
}
if (Input.GetKey(KeyCode.A) && transform.position == pos)
{ // Left
animator.SetInteger("isWalking", 4);
pos += Vector3.left;
}
if (Input.anyKey == false)
animator.SetInteger("isWalking", 0);
transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
}
}
Thanks to @Nitro557 i had a new idea instead of basicly teleporting the player around i used a whole different method of moving the player here is the script:
public float runSpeed = 2.0f;
private Rigidbody2D body;
private Animator animator;
private float horizontal;
private float vertical;
private float moveLimiter = 0.7f;
void Start()
{
body = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
void Update()
{
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
if(Input.GetKeyDown(KeyCode.LeftShift))
{
runSpeed += 0.5f;
}
}
private void FixedUpdate()
{
if (horizontal != 0 && vertical != 0)
{
horizontal *= moveLimiter;
vertical *= moveLimiter;
}
body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);
// Up
if (Input.GetKey(KeyCode.W))
animator.SetInteger("isWalking", 1);
// Down
if (Input.GetKey(KeyCode.S))
animator.SetInteger("isWalking", 2);
// Right
if (Input.GetKey(KeyCode.D))
animator.SetInteger("isWalking", 3);
// Left
if (Input.GetKey(KeyCode.A))
animator.SetInteger("isWalking", 4);
if (Input.anyKeyDown == false)
animator.SetInteger("isWalking", 0);
body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);
}