Hello again!

It’s time for another ‘How To” post – because, well, we ran into a little problem with being able to stand on top of an object that’s been… childed? … to a moving platform. Check that post out here, if you’re interested!

What was the problem? Well, if you have an object that’s attached to a moving platform, that’s all well and good, but what if you want to put something else on top of that object? Let’s say you try to jump on it, but because your character isn’t made a child of the platform, the platform – and it’s children – are going to just move right out from under your player, making life difficult. We needed a way to make it to where your character would stick to the platform, and be infinitely stackable – so you could have a whole stack of objects that behave like this.

Here’s an example video to explain what’s going on (annnnnd give the first sneak preview of the main mechanic behind Run Die Repeat. Oops. =D)

As you can see, in the first half, it’s almost impossible to stand still as the platform moves. So what are we to do?

Let’s open up our platform controller code.  Here’s what we used to have:

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

public class PlatformExample : MonoBehaviour {

    private void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.transform.tag.Contains("Platform"))
        {
            transform.parent = collision.transform;
            isAttached = true;
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.transform.tag.Contains("Platform"))
        {
            transform.parent = null;
            isAttached = false;
        }
    }
}

That’s all great and all – it’s certainly doing a great job at keeping the dead guy in place. But the main guy isn’t behaving.  Let’s add a little bit of code:

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

public class PlatformExample : MonoBehaviour {

    public bool isAttached = false;
   

    private void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.transform.tag.Contains("Platform"))
        {
            transform.parent = collision.transform;
            isAttached = true;
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.transform.tag.Contains("Platform"))
        {
            transform.parent = null;
            isAttached = false;
        }
    }

    private void OnCollisionStay2D(Collision2D collision)
    {
        if (collision.transform.tag.Contains("Dead_Player"))
        {
            if (collision.gameObject.GetComponent<PlatmoreEnterExitScript>().isAttached == true)
            {
                transform.parent = collision.transform;
 isAttached = true;
            }
        }
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.transform.tag.Contains("Dead_Player"))
        {
            if (collision.gameObject.GetComponent<PlatmoreEnterExitScript>().isAttached == true)
            {
                transform.parent = null;
 isAttached = false;
            }
        }
    }


}

 

Basically, what we’re doing is making the player a child of the deadplayer! The best thing is – this will continuously stack on all objects that have this script because of the little variable we added for “isAttached” – if the object is attached to another object, it will carry that through to the top of the stack!

Does this help you at all? Let me know below!

Liked it? Take a second to support Run Die Repeat | Platformer Game Coming Soon to PC, Mac, and Steam on Patreon!
  • Facebook
  • Twitter
  • Google+
  • Gmail

Pin It on Pinterest

Get the Run Die Report!

Follow the journey of the development of Run Die Repeat!

You have Successfully Subscribed!