Having scripts affect each other in sequence
I'm working on a 2D game where there are door-like walls that can be opened by pressing a switch. I wanted these doors to be set up as sprites stacked up on one another; that way I can make each door as tall or small as they need to be. However, I'm having problems with the doors opening correctly. I wanted them to open from the bottom up, and have the switch set to only open the bottom segment of the door; however, when I press the switch, all segments of the door open at the same time. Also, when I come off the switch, it should close the door, but it doesn't. The door stays open. How do I get the door to open from the bottom up, and close when the switch isn't being pressed down?
Here's the code for the door:
public class LockedWall : MonoBehaviour {
public Animator anim;
private Collider2D collision;
public GameObject segmentAbove;
public GameObject segmentBelow;
public bool isBottom;
public bool isTop;
public bool isOpen;
void Start()
{
anim = GetComponent<Animator>();
collision = GetComponent<Collider2D>();
isOpen = false;
if(segmentBelow.tag != "LockedWall")
{
isBottom = true;
}
if (segmentAbove.tag != "LockedWall")
{
isTop = true;
}
if (segmentBelow.tag == "LockedWall")
{
isBottom = false;
segmentBelow.GetComponent<LockedWall>();
}
if (segmentAbove.tag == "LockedWall")
{
isTop = false;
segmentBelow.GetComponent<LockedWall>();
}
}
public void DoorBottomOpens()
{
if (isBottom)
{
anim.SetBool("open", true);
collision.enabled = false;
segmentAbove.GetComponent<LockedWall>().DoorOpens();
IsOpen();
}
}
public void DoorOpens()
{
if (!isBottom)
{
anim.SetBool("open", true);
collision.enabled = false;
IsOpen();
}
}
public void DoorTopCloses()
{
if (isTop)
{
anim.SetBool("open", false);
collision.enabled = true;
segmentBelow.GetComponent<LockedWall>().DoorCloses();
IsClosed();
}
}
public void DoorCloses()
{
if (!isTop && segmentAbove.GetComponent<LockedWall>().isOpen == false)
{
anim.SetBool("open", false);
collision.enabled = true;
IsClosed();
}
}
void IsOpen()
{
if (!isTop)
{
segmentAbove.GetComponent<LockedWall>().DoorOpens();
}
}
void IsClosed()
{
if (!isBottom)
{
segmentBelow.GetComponent<LockedWall>().DoorCloses();
}
}
}
And the script for the switch:
public class Switch : MonoBehaviour {
private Animator anim;
[SerializeField]
private bool redSwitch;
[SerializeField]
private GameObject doorTrig;
void Start()
{
anim = GetComponent<Animator>();
}
void OnTriggerStay2D()
{
anim.SetBool("down", true);
doorTrig.GetComponent<LockedWall>().DoorBottomOpens();
}
void OnTriggerExit2D()
{
if (!redSwitch)
{
return;
}
anim.SetBool("down", false);
doorTrig.GetComponent<LockedWall>().DoorTopCloses();
}
}
Your answer
Follow this Question
Related Questions
Sprite animation cannot be play when player retry Unity 0 Answers
Syncing 2D Sprite Animation 0 Answers
How to do these energy spheres 0 Answers
Spriter 2D animation to Unity 1 Answer
How do I display UI animation? 0 Answers