如何使我的按钮在碰撞时发生变化 [英] How to make my button change on collision

查看:72
本文介绍了如何使我的按钮在碰撞时发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在尝试将我的精灵按钮从绿色变为红色,并在播放器与它碰撞时向后移动.我有一个boxcollider2d触发器,还有一个脚本

Hey I am trying to make my sprite button go from green to red and backwards when my player collides with it. I have a boxcollider2d as is trigger, and a script

script: https://gyazo.com/ec64a2bca5b23526c6949bf18cb50a0d

我想是其中的一部分,但是与按钮碰撞时我什么也没得到,任何人都可以启发我解决这个问题.

I think this is some of it but I dont get anything at all when colliding with the button can anyone enlighten me on this problem would be very appreciated.

预先感谢

推荐答案

重要的是要区分碰撞器和触发器之间的区别.

It is important to distinguish the difference between Colliders and Triggers.

物理系统使用对撞机,以使对象不会相互穿透或重叠.另一方面,进行了触发器,以便您可以检查是否有重叠区域,例如声波环".索尼克收集了戒指,但它们并没有阻止他过去,可以将其视为触发因素.如果它们是对撞机,那么声音会跳入其中并反弹.

Colliders are used by the Physics system in order to make it so object do not penetrate or overlap each other. Triggers on the other hand are made so that you can check for overlapping areas, an example is a Ring in Sonic. Sonic collects the rings but they do not stop him from passing, think of these as triggers. If they were colliders sonic would jump into them and bounce off.

在Unity中,创建触发器的过程与创建对撞机的过程相同,仅需增加一个步骤,即标记一个复选框.

In Unity the process for creating a trigger is the same for creating a collider, with 1 additional step, and that is marking a check box.

因此,了解每种情况下应使用的功能也很重要.

As such it is also important to know which functions you should use in each case.

OnCollision ...对于碰撞器,(或未标记为触发器的碰撞器)

OnCollision... For Colliders, (Or Colliders not marked as triggers)

OnTrigger ...(用于触发器),(将对撞机统一标记为触发器会使其成为触发器.)

OnTrigger... for Triggers, (Marking a collider in unity as a trigger makes it a trigger.)

要添加更多细节,对于正确的游戏尺寸使用正确的碰撞器类型也很重要,因此2d碰撞器/触发器不会与3d碰撞器/触发器进行交互.物理成分更多:2d刚体不适用于3d碰撞器,而3d刚体不适用于2d碰撞器/触发.

To just add a bit more detail, It is also important to use the correct collider types for the correct dimensions of the game, 2d Colliders/Triggers do not interact with 3d Colliders/Triggers. More so physics components: 2d Rigidbodies do not works with 3d colliders, and 3d rigidbodies do not work with 2d colliders/triggers.

因此,每个2d,3d,碰撞和触发都有功能.

As such there are functions for each 2d, 3d, collision, and trigger.

另一个重要因素是要确保是否要使用这些功能,至少其中一个对象必须具有正确类型(2d或3d)的刚体.

Another important factor is to ensure if you want to use these functions atleast one of the objects have to have a rigidbody of the correct type(2d or 3d).

因此,您遇到的问题是将OnCollisionEnter2D用于标记为触发器的对撞机",在这种情况下,不会调用OnCollisionEnter2D,但会调用OnTriggerEnter2D.

As such the problem you were running into was using OnCollisionEnter2D for a "collider" that was marked as a trigger, in this case OnCollisionEnter2D is not called, but OnTriggerEnter2D is called.

这篇关于如何使我的按钮在碰撞时发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆