Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to reset position after collision

Discussion in 'Scripting' started by darthcoder567, Oct 12, 2022.

  1. darthcoder567

    darthcoder567

    Joined:
    Aug 20, 2022
    Posts:
    45
    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
    Code (CSharp):
    1. void OnCollisonEnter2D(Collision2D collision)
    2.     {
    3.         _rigidbody2D.position = _startPosition;
    4.         _rigidbody2D.isKinematic = true;      
    5.     }
    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?
     
  2. TheDevloper

    TheDevloper

    Joined:
    Apr 30, 2019
    Posts:
    69
    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.
     
  3. darthcoder567

    darthcoder567

    Joined:
    Aug 20, 2022
    Posts:
    45
    I put a debug log in.I dont know if I put it correctly but this is it:
    Code (CSharp):
    1.   void OnCollisonEnter2D(Collision2D collision2D)
    2.     {
    3.         _rigidbody2D.position = _startPosition;
    4.         _rigidbody2D.isKinematic = true;
    5.         Debug.Log("hi");
    6.     }
    it didnt put anything in my debug log but my character is colliding with the squares when it moves into them.
     
  4. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    Make sure both colliders arent marked as trigger and that both are on the same layer
     
  5. darthcoder567

    darthcoder567

    Joined:
    Aug 20, 2022
    Posts:
    45
    how do I check to make sure they are on the same layer
     
  6. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    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.
     
  7. darthcoder567

    darthcoder567

    Joined:
    Aug 20, 2022
    Posts:
    45
    they all have the layer default but its still not working
     
  8. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    Is there a rigidbody on one of the objects? Is trigger not marked on the collider for both objects?
     
  9. darthcoder567

    darthcoder567

    Joined:
    Aug 20, 2022
    Posts:
    45
    Is trigger is unchecked for all objects
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter2D.html

    "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.
     
    Johan_Liebert123 likes this.
  11. darthcoder567

    darthcoder567

    Joined:
    Aug 20, 2022
    Posts:
    45
    they both have is trigger unchecked
     
  12. darthcoder567

    darthcoder567

    Joined:
    Aug 20, 2022
    Posts:
    45
    also that code is excatly what the tutorial told me to put
     
  13. chrische5

    chrische5

    Joined:
    Oct 12, 2015
    Posts:
    52
    Hello

    Your code is not right. Take care of spelling.

    Christoph
     
  14. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    377
    OnCollisionEnter2D (you forgot "i")
     
  15. ninjakiller0511

    ninjakiller0511

    Joined:
    May 24, 2020
    Posts:
    1
    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

    Code (CSharp):
    1. private void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.         transform.position = _startPosition;
    4.         _rigidbody2D.isKinematic = true;    
    5.         Debug.Log("<color=red> OnCollisionEnter2D </color>")
    6.     }
     
  16. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    Omg how did I not see that :(
     
  17. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    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. :)
     
  18. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    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.
     
  19. darthcoder567

    darthcoder567

    Joined:
    Aug 20, 2022
    Posts:
    45
    oh i forgot the I thanks for helping me figure this out!:)