How to reset position after collision

I was watching a tutorial on unity and it shoed how to reset my characters position after a collsion This is the code That was supposed to work

void OnCollisonEnter2D(Collision2D collision)
    {
        _rigidbody2D.position = _startPosition;
        _rigidbody2D.isKinematic = true;      
    }

This doesnt give me any errors but it just doesnt reset my character to the start position after I collide with something.How do I make this work?

first of all use Debug.log and check if the collider happens in the first place.
second better change transform.position and rigidbody position also.

I put a debug log in.I dont know if I put it correctly but this is it:

  void OnCollisonEnter2D(Collision2D collision2D)
    {
        _rigidbody2D.position = _startPosition;
        _rigidbody2D.isKinematic = true;
        Debug.Log("hi");
    }

it didnt put anything in my debug log but my character is colliding with the squares when it moves into them.

Make sure both colliders arent marked as trigger and that both are on the same layer

how do I check to make sure they are on the same layer

In the inspector, on the top right corner, you’ll see a dropdown called layer, check if the layer is the same with both GameObjects, according to Physics 2D, I’m pretty sure the gameobjects have to be on the same layer to be able to collide.

they all have the layer default but its still not working

Is there a rigidbody on one of the objects? Is trigger not marked on the collider for both objects?

Is trigger is unchecked for all objects

“OnCollisonEnter2D” isn’t the correct method name spelling so this will never be called by Unity so hopefully this isn’t what’s in your code. You spell “Collision” correctly when you typed “Collision2D” though.

You wouldn’t have this problem if you had an IDE like Visual Studio set-up as it’d type this for you.

1 Like

they both have is trigger unchecked

also that code is excatly what the tutorial told me to put

Hello

Your code is not right. Take care of spelling.

Christoph

OnCollisionEnter2D (you forgot “i”)

check all of this in your project:

-Both objects must have colliders (2D)
-At least one of them has a rigidbody (2D)

And then copy this code into your project

private void OnCollisionEnter2D(Collision2D collision)
    {
        transform.position = _startPosition;
        _rigidbody2D.isKinematic = true;     
        Debug.Log("<color=red> OnCollisionEnter2D </color>")
    }

Omg how did I not see that :frowning:

Sorry to be critical but I think it’s constructive criticism; the fact that I told you exactly what was wrong but you didn’t read what I put, clearly shows that you are not being attentive enough to details and are looking but not reading.

A simple typo should not cause you so much pain and involve so many other devs. To make things easier, I would suggest reading details and being more careful. :slight_smile:

Don’t use this code, it’s flawed. You don’t modify the Transform, that won’t change the body position until the next simulation step; change the body position directly and let the Rigidbody2D do what it’s meant to do which is write to the Transform.

oh i forgot the I thanks for helping me figure this out!:slight_smile: