Help with unsticking objects

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

void OnCollisionEnter(Collision collision) {
    collision.gameObject.transform.parent = gameObject.transform;
}

But now i can unstick them any ideas ?
Thanks

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.

1 Like

Yeah have a special collider on the top of the other character, here is the full script if you want a look

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerHead : MonoBehaviour
{

    bool catOnHead = false;
    public Transform catPos;

    // Update is called once per frame
    void Update()
    {


    }
    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.collider.tag == "Cat")
        {
            collision.gameObject.transform.parent = gameObject.transform;
            catOnHead = true;
        }
    }
    void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.collider.tag == "Cat")
        {
            catOnHead = false;
        }
    }
}

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

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

You can just set it to null if you don’t have any special parent you want to reset it back to:

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.

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

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

1 Like