- Home /
Question by
zZagro · 4 days ago ·
collider2d-platformercollider2dtilemap2d-physics
Player Gets Stuck On TilemapCollider2D
Hey, I'm new to game development in Unity.
I wanted to make a 2d platformer with basic movement and the environment drawn with a tile palette but my player always gets stuck on the single box colliders of the tilemap collider. I would appreciate any help :)
Here's my code if it helps:
public class PlayerMovement : MonoBehaviour
{
[SerializeField] GameObject player;
[SerializeField] LayerMask groundLayermask;
private Movement movement;
private Rigidbody2D rigidBody;
private BoxCollider2D boxCollider;
void Start()
{
movement = GetComponent<Movement>();
rigidBody = player.GetComponent<Rigidbody2D>();
boxCollider = player.GetComponent<BoxCollider2D>();
}
void Update()
{
player.transform.eulerAngles = new Vector3(0, 0, 0);
if (Input.GetKeyDown(KeyCode.Space) && IsGrounded()) {
rigidBody.AddForce(player.transform.up * movement.jump, ForceMode2D.Impulse);
}
}
void FixedUpdate() {
if (Input.GetKey(KeyCode.D)) {
player.transform.position += player.transform.right * movement.speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A)) {
player.transform.position -= player.transform.right * movement.speed * Time.deltaTime;
}
}
private bool IsGrounded() {
float extraHeightTest = 0.02f;
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center, boxCollider.bounds.size, 0f, Vector2.down, extraHeightTest, groundLayermask);
if (raycastHit.collider != null)
{
Debug.Log(raycastHit.collider);
}
return raycastHit.collider != null;
}
}
Comment