[英]Unity 2D Platforms Show on Collision
How can I make a group of platforms appear when colliding with it's trigger? 与触发器触发时,如何使一组平台出现?
I would like to walk into the trigger, which will turn on the 'StarPlatforms' in the inspector, revealing every thing that is a child of it. 我想进入触发器,该触发器将在检查器中打开“ StarPlatforms”,并显示所有属于它的子对象。 I have only got it to do the exact opposite:
我只是做相反的事情:
Children of StarPlatfroms StarPlatfroms的孩子
The script on the StarPlatforms : StarPlatforms上的脚本 :
public class AppearTrigger : MonoBehaviour {
private GameObject StarPlatforms;
void Start() {
StarPlatforms = GameObject.Find("StarPlatforms");
StarPlatforms.gameObject.SetActive (true);
}
void OnTriggerStay2D (Collider2D col) {
if (col.gameObject.tag == "Player") {
StarPlatforms.gameObject.SetActive (false);
}
}
}
I have tried swapping the .SetActives too false then true and it unchecks in the inspector on Play but stays hidden on collision with the trigger. 我尝试过将.SetActives交换为false,然后为true,并且它在Play的检查器中未选中,但是在与触发器碰撞时保持隐藏状态。
Thank you for your time. 感谢您的时间。
ps: this is my first go at Unity & C# ps:这是我第一次参加Unity&C#
SetActive just sets whether it is part of the main game loop or not. SetActive只是设置它是否是主游戏循环的一部分。 To make a platform invisible or visible use it's Renderer component and deactivate that instead.
要使平台不可见或可见,请使用它的Renderer组件并将其停用。
gameObject.GetComponent<SpriteRenderer>().enabled = false;
I would like to walk into the trigger, which will turn on the 'StarPlatforms' in the inspector.
我想进入触发器,它将在检查器中打开“ StarPlatforms”。 I have tried swapping the .SetActives too false then true
我尝试过将.SetActives交换为false,然后为true
You seem to be guessing which one works. 您似乎正在猜测哪个有效。 This is how
SetActive
work: 这是
SetActive
工作方式:
To turn on or activate an object, pass true
to SetActive
. 要打开或激活对象,
SetActive
true
传递给SetActive
。 To turn it off or deactivate it, pass false
to it. 要关闭或停用它,请将
false
传递给它。 Also, this should be done in the OnTriggerEnter2D
function not OnTriggerStay2D
. 另外,这应该在
OnTriggerEnter2D
函数而不是OnTriggerStay2D
。
The OnTriggerStay2D
function is called when collision each frame collision is still touching. 当每个帧碰撞仍在发生碰撞时,将调用
OnTriggerStay2D
函数。 The OnTriggerEnter2D
function is called when there is a collision. 发生冲突时将调用
OnTriggerEnter2D
函数。 OnTriggerExit2D is called when there is no longer a collision. 当不再有冲突时,将调用OnTriggerExit2D。
void OnTriggerEnter2D (Collider2D col)
{
if (col.CompareTag("Player"))
{
StarPlatforms.gameObject.SetActive (true);
}
}
If you also want to turn it of or de-activate it when they no longer touch, pass false
to the SetActive
function in the OnTriggerExit
function. 如果您也想在不再触摸时将其关闭或停用,请将
false
传递给OnTriggerExit
函数中的SetActive
函数。
void OnTriggerExit2D(Collider2D col)
{
if (col.CompareTag("Player"))
{
StarPlatforms.gameObject.SetActive (false);
}
}
Not related to your issue but it's a good practice to use CompareTag
instead of gameObject.tag
. 与您的问题无关,但是最好使用
CompareTag
而不是gameObject.tag
。
This is what worked: 这是有效的:
public class AppearTrigger : MonoBehaviour {
GameObject[] StarPlatfromArray;
void Start() {
StarPlatfromArray = GameObject.FindGameObjectsWithTag("Cave");
for (int i = 0; i < StarPlatfromArray.Length; i=i+1) {
StarPlatfromArray [i].gameObject.GetComponent<BoxCollider2D> ().enabled = false;
StarPlatfromArray [i].gameObject.GetComponent<SpriteRenderer> ().enabled = false;
}
}
void OnTriggerEnter2D (Collider2D col) {
StarPlatfromArray = GameObject.FindGameObjectsWithTag("Cave");
for (int i = 0; i < StarPlatfromArray.Length; i++) {
StarPlatfromArray [i].gameObject.GetComponent<BoxCollider2D> ().enabled = true;
StarPlatfromArray [i].gameObject.GetComponent<SpriteRenderer> ().enabled = true;
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.