简体   繁体   English

当玩家与触发器发生碰撞时,如何启用游戏 object 的组件?

[英]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.

相关问题 当敌人与不同的 object 发生碰撞时,我怎样才能让玩家失去健康? - How can I make it so that the player loses health when an enemy collides with a different object? 玩家与 object 发生碰撞后,如何再次重生它? - How can I respawn the object again after player collides with it? 当游戏对象与主角碰撞时,如何破坏整个预制件? - How to destroy the whole prefab when the game object collides with the main character? 播放器与标记对象碰撞时消失 - Player disappears when it collides with a tagged object 在 Unity 中与游戏对象发生碰撞时,如何让我的玩家加速? - How do I make my player speed up when collide with a game object in Unity? 当玩家与物体碰撞时增加滑块。 统一 - Increase slider when player collides with object. Unity 当玩家与另一个 object 碰撞并按下键时,有没有办法加分? - Is there a way to add points when a player collides with another object and key pressed? 我怎样才能做到这一点,当我的玩家与敌人碰撞时,它会禁用移动输入,直到玩家接触地面? - How can I make it so when my player collides with an enemy, it disables movement input until player touches the ground? 与物体碰撞时如何制作文本透明度? - How to Make Text Transparencies when Collides with object? 当玩家与障碍物碰撞时如何使相机抖动? - How to make camera shake when player collides with an obstacle?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM