Simulating 2D depth in a sidescroller
I am developing a 2D sidescroller. One of the basic mechanics of the game is that the player can choose between being on the foreground of the level or on the background of the level (deviating from the obstacles accordingly).
Since I´m developing it in pure 2D form, I´m having to "simulate" the depth of the level.
For that, I created two scripts:
One of them turns off the FOREGROUND objects collider (TurnOffCollisionForeground)
The other turns off the BACKGROUND objects collider (TurnOffCollisionBackground)
using UnityEngine; using System.Collections;
public class TurnOffCollisionForeground : MonoBehaviour
{
new BoxCollider2D boxCollider;
// Use this for initialization
void Start()
{
boxCollider = GetComponent<BoxCollider2D>();
}
public void OnCollisionEnter2D(bool state)
{
if(state == true)
{
boxCollider.enabled = false;
return;
}
if (state == false)
{
boxCollider.enabled = true;
return;
}
}
// Update is called once per frame
void Update()
{
}
}
using UnityEngine;
using System.Collections;
public class TurnOffCollisionBackground : MonoBehaviour
{
new BoxCollider2D boxCollider;
// Use this for initialization
void Start()
{
boxCollider = GetComponent<BoxCollider2D>();
}
public void OnCollisionEnter2D(bool state)
{
if (state == true)
{
boxCollider.enabled = true;
return;
}
if (state == false)
{
boxCollider.enabled = false;
return;
}
}
// Update is called once per frame
void Update()
{
}
}
To check in what layer of depth the player is, I wrote the script CheckingLayerScript (which is attached to the player). In this script, inside the Checking() function (which is repeatedly called in Update() ) I check for player answer regarding the choice of layer - which he transits between by pressing the space bar).
using UnityEngine;
using System.Collections;
public class CheckingLayerScript : MonoBehaviour
{
public bool isOnForeground;
public bool isOnBackground;
TurnOffCollisionForeground front;
TurnOffCollisionBackground back;
// Use this for initialization
void Start()
{
isOnForeground = true;
isOnBackground = false;
}
void Checking()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (isOnForeground == true && isOnBackground == false)
{
isOnForeground = false;
isOnBackground = true;
front.OnCollisionEnter2D(false);
back.OnCollisionEnter2D(true);
return;
}
if (isOnForeground == false && isOnBackground == true)
{
isOnForeground = true;
isOnBackground = false;
front.OnCollisionEnter2D(true);
back.OnCollisionEnter2D(false);
return;
}
}
}
// Update is called once per frame
void Update()
{
Checking();
}
}
In this script, I tried to arrange a way so the game knows when the player is on the foreground (isOnForeground variable - Note: the player ALWAYS starts at the foreground) and when the player is on the background (isOnBackground variable).
Now here´s my problem:
Inside Checking(), when the player presses the space bar, besides rearranging the status where he is (on foreground or background) I also try to call the OnCollisionEnter2D() function from the TurnOffCollisionForeground script and the OnCollisionEnter2D() from the TurnOffCollisionBackground script so I can turn off the collider from the objects that have said scripts.
To do that, the way I tried was to create an object from both of those scripts on my CheckingLayerScript script (the objects being front and back) and using them to call the OnCollisionEnter2D() function to turn off the colliders.
But that´s not working. I´m pretty new to Unity/C# development so it might just be dumb ignorance regarding calling functions from other scripts, but if you guys could help me it would be hugely appreciated.