So – how about that title?

Perhaps… a bit excessive. But that’s ok. We’re excited. We made something that was being difficult work!

BEHOLD!

Image result for doofenshmirtz behold
  • Facebook
  • Twitter
  • Google+
  • Gmail

So – what’s actually going on here? We have a moving platform that causes the character to move with it, while retaining the ability to walk and jump and stuff (sorry, we failed to demonstrate that in the video.  Our bad).

Basically, we created a new script:

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

public class PlatformEnterExitScript : MonoBehaviour {

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

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

And we attached that script to our character object.

Then, we took the GameObject that contained the two parts of the platform and added a BoxCollider2D to it, set as trigger, and tagged the whole construct as “Platform”

Now, whenever the character Object sits still on the platform, it inherits it’s transform properties, moving in time with the platform.  How awesome, right?

Here’s a bit more on why it works:


            transform.parent = collision.transform;
      

This magical little line basically changes the relationship of the character to the game – assigning him/her to the platform itself, which is why the “TriggerExit” is necessary – if you don’t remove the relationship, the character will act all kinds of funny after they get off the platform.  Try it – it’s kinda fun =D

Hope that helps you in your own Unity development, or is at least entertaining!

Like what you see with Run Die Repeat? Consider becoming a Patron – because you’re awesome. Rock on awesome dude(tte). Rock on. Patreon for Run Die Repeat

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!