Hello you all!
I recently started programming in Unity, since I always worked on GameMaker, I think it’s a good decision to also learn Unity, as it’s more famous than GM and basically almost all the companies uses it.
Nevermind, I started my project and I am working on my player’s hitbox and collisions with Tilemaps. It works very well for jumping and running, but there’s a little problem when I am in front of a wall and I try to jump it, let me show to you:
on8nmm
As you can see, I can move and jump freely, but if I jump just next to a wall, my player clips there, and then I can’t jump again, so I think something is not good here. I suppose is some problem with the Collider 2D? I had the same issue in GM and the problem was the Collision Mask, but I don’t what is causing this in Unity.
In case you need more info, here’s my player movement code:
public int velocity= 7;
public int jump= 100;
private float movement;
private int scale = 1;
private bool canJump;
private Rigidbody2D rb;
private Animator anim;
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void Update()
{
//Jump
if (Input.GetKeyDown(KeyCode.Z) && canJump)
{
rb.AddForce(Vector2.up * jump, ForceMode2D.Impulse);
canJump = false;
}
//Check for movement
if (Input.GetKey("right"))
{
movement = 1;
}
else if (Input.GetKey("left"))
{
movement = -1;
}
else movement = 0;
//Running animation
anim.SetBool("running", movement != 0);
//Scaling in direction
if (movement != 0)
{
scale = movement < 0 ? -1 : 1;
}
transform.localScale = new Vector3(scale, 1, 1);
}
private void FixedUpdate()
{
rb.velocity = new Vector2(movement * velocity, rb.velocity.y);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.transform.tag == "floor")
{
canJump = true;
}
}
Appreciate the help!
Thanks.