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. Dismiss Notice

Help with unsticking objects

Discussion in 'Scripting' started by StevenAtkinson82, Feb 27, 2021.

  1. StevenAtkinson82

    StevenAtkinson82

    Joined:
    Feb 29, 2020
    Posts:
    17
    I have two players in my 2d unity game both moving independently but when one jumps on the others head it should stay there till it jumps off. I managed to get it to stick using

    Code (CSharp):
    1. void OnCollisionEnter(Collision collision) {
    2.     collision.gameObject.transform.parent = gameObject.transform;
    3. }
    But now i can unstick them any ideas ?
    Thanks
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You can just unparent it by settings the parent to whatever needs to be the parent. If there's no object you want to parent it to, setting a parent to "null" will effectively make it a top-level object in the world (which will be represented as top-level object in the hierarchy). If there is a parent, for instance your own "world parent", set that one as the parent.

    The mechanism you can use - based on your current logic - is "OnCollisionExit". However, I would recommend checking whether or not you should (un-)parent the character in the first place, because these collision messages are raised for all collisions that occur and you probably don't want to parent the object to everything it collides with. You might also need additional logic to detect that you're actually on top of another character, unless you have a special collider setup.
     
    Putcho likes this.
  3. StevenAtkinson82

    StevenAtkinson82

    Joined:
    Feb 29, 2020
    Posts:
    17
    Yeah have a special collider on the top of the other character, here is the full script if you want a look

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerHead : MonoBehaviour
    6. {
    7.  
    8.     bool catOnHead = false;
    9.     public Transform catPos;
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.  
    15.  
    16.     }
    17.     void OnCollisionEnter2D(Collision2D collision)
    18.     {
    19.         if (collision.collider.tag == "Cat")
    20.         {
    21.             collision.gameObject.transform.parent = gameObject.transform;
    22.             catOnHead = true;
    23.         }
    24.     }
    25.     void OnCollisionExit2D(Collision2D collision)
    26.     {
    27.         if (collision.collider.tag == "Cat")
    28.         {
    29.             catOnHead = false;
    30.         }
    31.     }
    32. }
    So i just need to know what to put in the exit 2d collision bit to make the cat have his own movement back.

    Thanks for your help
     
  4. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    if the cat need to jump off, the question is does the jump mechanic good enough to trigger OnCollisionExit2D
    i recommend to write a debug.Log"Cat is jumped and it leave the collider" in side OnCollisionExit2D, just a simple up jump and land on the head and see what the console will say
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You can just set it to null if you don't have any special parent you want to reset it back to:
    Code (csharp):
    1. collision.gameObject.transform.parent = null;
    There's also SetParent, which you can use if you need more control about how the transform values are affected by the change.

    Though as I said, it depends what you want to parent it to. Null will unparent it completely so that it the object becomes a root object itself.

    Small side note: Use CompareTag rather than the equality operator == when you want to compare tags. It doesn't make a difference in how things work, but it's a small improvement on the technical side.
     
  6. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    i believe you have turn the cat movement off once it land on the head including jump.

    here how i will solve:
    1. jump muss not be turn off
    2. lock the movement when it on the head but turn it on when the cat start jump
    3. once the jump and move mid air working you can now check if the OnCollisionExit2D working
    4. if it work all you have do is unparent like Suddoha show you
     
  7. StevenAtkinson82

    StevenAtkinson82

    Joined:
    Feb 29, 2020
    Posts:
    17
    collision.gameObject.transform.parent = null;
    fixed it so thank you both for commenting it is very much appreciated, will definately be back again at somepoint lol thanks again
     
    Putcho likes this.