Box Collider Blocks Movement,Box Collider Blocks movement after collision
So I'm making a 2d game, the map has box colliders, but when the player object hits the box the movement stops working
Player Movement Script :
using UnityEngine; using System.Collections; public class PlayerMove : MonoBehaviour { public float speed = 0.4f; Vector2 dest = Vector2.zero; void Start() { dest = transform.position; } void FixedUpdate() { // Move closer to Destination Vector2 p = Vector2.MoveTowards(transform.position, dest, speed); GetComponent<Rigidbody2D>().MovePosition(p); // Check for Input if not moving if ((Vector2)transform.position == dest) { if (Input.GetKey(KeyCode.UpArrow) && valid(Vector2.up)) dest = (Vector2)transform.position + Vector2.up; if (Input.GetKey(KeyCode.RightArrow) && valid(Vector2.right)) dest = (Vector2)transform.position + Vector2.right; if (Input.GetKey(KeyCode.DownArrow) && valid(-Vector2.up)) dest = (Vector2)transform.position - Vector2.up; if (Input.GetKey(KeyCode.LeftArrow) && valid(-Vector2.right)) dest = (Vector2)transform.position - Vector2.right; } // Animation Parameters Vector2 dir = dest - (Vector2)transform.position; GetComponent<Animator>().SetFloat("DirX", dir.x); GetComponent<Animator>().SetFloat("DirY", dir.y); } bool valid(Vector2 dir) { Vector2 pos = transform.position; RaycastHit2D hit = Physics2D.Linecast(pos + dir, pos); return (hit.collider == GetComponent<Collider2D>()); } }
Your answer
Follow this Question
Related Questions
How to fix player bumping into the wall? 0 Answers
A box collider 2D (Is Trigger marked) stops my player from moving which has rigidbody 2D 1 Answer
"Sneaking" in a Grid Based Movement Game? 0 Answers
How to make player fall through platforms? 0 Answers
Player making "random jumps" while is moving at random places (Unity 2D) 0 Answers