I have a problem with the collision of two 2D sprites.
I leave you a video to make you understand my problem:
As you can see, the collision is not perfect, and the square many times enters the block below. The collision is not precise.
I leave here also the code I use:
public float jumpHeight = 40f;
public float gravity = 1f;
float JumpVelocity;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
AddGravityToPlayer();
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Block")
{
if (rb.velocity.y <= 0){
JumpVelocity = gravity * jumpHeight;
rb.velocity = new Vector2(0, JumpVelocity);
}
}
}
private void AddGravityToPlayer()
{
rb.velocity = new Vector2(0, rb.velocity.y - (gravity * gravity));
}
Where am I wrong?
Edit:
I tried to set up the collision detection of the player on continuos, but the result does not change.