- Home /
2D Collision problem
I have a problem with the collision of two 2D sprites. I leave you a video to make you understand my problem: https://www.youtube.com/watch?v=VR3DXaKEHdY
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.
Answer by Vega4Life · Dec 01, 2018 at 06:16 PM
The problem is you are using a trigger. Triggers allow for objects to go through them, so once the OnTrigger fires, its too late, the colliders are through each other.
Switch to void OnCollisionEnter2D(Collision2D other), and use the same code you have for OnTriggerEnter. This should do the trick.
Your answer
![](https://koobas.hobune.stream/wayback/20220612183114im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Collision on specific Frames 1 Answer
Multiple Cars not working 1 Answer
[c#]collision script not working 1 Answer
Collider just for Tag (2D) (C#) 1 Answer
Destroy object on touch of object with specific class 3 Answers