One-Way Platforms, Enabling/Disabling

Hi all,

(2D Physics being used)

My Player object is a Box Collider on top of a Circle Collider, where there is a small gap between the bottom of the Box and the top of the Circle.

I’m trying to make old-school one-way platforms, the sort where you can jump up through them with no resistance, but which can also be landed on from above like normal platforms. I’m doing this by having a platform with a BoxCollider2D, which starts off disabled, and a script. This platform has a child object, sensor, which contains a BoxCollider2D used as a Trigger. The BoxCollider2D of sensor is substantially larger than that of the platform, so that it is impossible to touch the platform without first touching the sensor. Sensor also has a script to deal with players passing through it. When the Player or enemy passes through (EXITS) the sensor, the BoxCollider 2D of the platform is enabled, and the Trigger box on sensor is disabled, as is the triggering script. When the Player ceases to Collide with the Platform after its activation, it is deactivated and the sensor’s script and Trigger box are re-activated.

Currently, this actually sort of works, but for some unfathomable reasons, when I turned it into a prefab and inserted copies into the scene, the copies did NOT work - they consistently fail to enable the Platform’s box collider, meaning that the platforms on them are just artwork.

Here is the code for the Platform:

using UnityEngine;
using System.Collections;

public class OneWayCollider : MonoBehaviour {
	[SerializeField] LayerMask whatIsOneWay;//determines what layers we should count as one-way, there ought only to be one such.
	float timeToExpire;
	public Transform newPlatform;
	Transform sensor;
	// Use this for initialization
	void Start () {
		sensor = transform.FindChild ("sensor");
	}
	
	// Update is called once per frame
	void Update () {

			sensor = transform.FindChild ("sensor");

		}
	void OnCollisionExit2D(Collision2D collision)
	{
		if (collision.gameObject.layer == 8 || collision.gameObject.layer == 10) {
			GetComponent<BoxCollider2D>().enabled = false;
			//this.enabled = false; //the current script
			sensor.GetComponent<BoxCollider2D>().enabled = true;
			sensor.GetComponent<oneWayTrigger>().enabled = true;
	}
	}
}

and here is the code for the Sensor:

using UnityEngine;
using System.Collections;

public class oneWayTrigger : MonoBehaviour {
	Transform parent;
	// Use this for initialization
	void Start () {
		parent = transform.parent;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	void OnTriggerExit2D()
	{
		parent.GetComponent<BoxCollider2D> ().enabled = true;
		this.enabled = false;//disables the script
		gameObject.GetComponent<BoxCollider2D> ().enabled = false;//disables the Trigger box collider
	}
}

Can anybody shed any light on the situation?

Thanks,
Cantus

I’ve been working on the same thing. and found a really nice and easy method, Im not sure what’s the problem with your script, maybe you’d prefer this way.
on my platform i have a child gameobject attached which has a trigger box collider, then, on my player i’ve got a few simple code lines, that whenever the player enters the child which is a trigger, the parent collider2D ill be disabled and when it exits the trigger the collider will re-enable.

	void OnTriggerEnter2D(Collider2D other){
		var curPlatform = other.transform.parent.GetComponent<Collider2D> ();
		Physics2D.IgnoreCollision (player.collider2D, curPlatform);
	}
	void OnTriggerExit2D(Collider2D other){
		if (player.rigidbody2D.velocity.y > 0) {
			var curPlatform = other.transform.parent.GetComponent<Collider2D> ();
			Physics2D.IgnoreCollision (player.collider2D, curPlatform, false);
		}
	}