[英]How do I enable a component of a game object when the player collides with a trigger?
I am making a game where you have to get from point A to point B without hitting any obstacles.我正在制作一个游戏,您必须在不碰到任何障碍的情况下从 A 点到达 B 点。 Some obstacles are moving and some are not.
有些障碍物在移动,有些则没有。 I decided to add a trigger in one of my levels that would enable two obstacles' scripts that would make them move.
我决定在我的一个关卡中添加一个触发器,以启用两个障碍物的脚本,使它们移动。 This is the script that is attached to the trigger:
这是附加到触发器的脚本:
private void OnCollisionEnter(Collision collision)
{
GameObject.Find("Obstacle (10)").GetComponent<MovingObjects>().enabled = true;
GameObject.Find("Obstacle (9)").GetComponent<MovingObjects>().enabled = true;
}
For some reason, this code along with the method do not seem to work.出于某种原因,此代码以及该方法似乎不起作用。 Nothing happens when the player passes through the trigger.
当玩家通过触发器时没有任何反应。 Why?
为什么?
Checkout the Collision matrix查看碰撞矩阵
Collision events (like OnCollisionEnter
) are not thrown on a Collider marked as isTrigger
.碰撞事件(如
OnCollisionEnter
)不会在标记为isTrigger
的 Collider 上引发。
Note that also afaik OnTriggerEnter
would only be called on the object which is not the trigger.请注意,afaik
OnTriggerEnter
也只会在不是触发器的 object 上调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.