12 第1页 | 共2 页下一页
返回列表 发新帖
查看: 6361|回复: 13
打印 上一主题 下一主题

[其他] unity3d中Trigger的使用探讨(一)

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2011-12-14 14:40:31 |只看该作者 |倒序浏览
原文标题为:Getting Tricky with Triggers,很有用的知识点研究。 Unity triggers are insanely useful.  If you aren’t using physics triggers–even if your game is seemingly non-physical–you’re missing out on some of the most powerful functionality in Unity.  Let’s take a look:



What’s a Trigger?



A trigger is a Collider that fires events as other Colliders enter and leave its collision volume, but without physically interacting with your scene. They are physically “invisible”.  If a Rigidbody “hits” a trigger, it passes right through, but the trigger produces OnTriggerEnter calls, when the object enters, OnTriggerStay calls, for as long as the object is inside the trigger, and OnTriggerExit calls, for when the object leaves the trigger.
Take a look at the Unity collider documentation for a more verbose explanation, as well as the collision chart for when collision events are called.
Where to Use Triggers?



We use triggers as:
Static Proximity Triggers

The most obvious trigger example is to use them as, well, actual proximity triggers. Some piece of game logic is***n when the player, or another object, reaches a point in space.  You could place a trigger in front of a door which causes the door to open as the player approaches.  You can easily test the entered Collider for some layer, tag, or Component to see if the trigger should***cute.  This is as sample as:
function OnTriggerEnter(other:Collider)

{

   // only process if we’re the player

   if(!other.CompareTag(“Player”))

      return;

   

   // does the player have the blue key?  if not, boo

   if(!Player.HasKey(“blue”))

      return;

      

   // actually open the door!

}

Radius Triggers

The first trick with triggers is to realize they can move. You can set a trigger as a child of an object–even another physical object like a Rigidbody–and then use the trigger to take action as other objects approach or exit a radius around your object of interest.
Take the use case of spawn points, for instance; your goal is to trigger an enemy spawn as a spawn point nears your player. You could place a script on your player that iterates all possible spawn points and spawns something if it’s near enough:
function FixedUpdate()

{

for(var spawn:SpawnPoint in allSpawns)

   if(Vector3.Distance(transform.position, spawn.transform.position < spawnRadius)

      spawn.DoSpawn();

}

The #1 reason for using triggers, over this “manual” method, is that triggers will be faster. Much faster! There are ways to make this example better–check the sqrMagnitude of the distance to avoid a square root, cache your Transform, etc–but the ***x of the problem remains: You have to***n this code every Update, FixedUpdate, or at least with enough frequency to respond interactively.
However, the physics engine is already checking for trigger collisions. It’s doing the work for you! Let it do its super-fast optimized thing; believe me, you can’t compete with PhysX on an optimization level. Instead of polling for spawn points in our radius ourselves, let the physics engine send events only when something enters our player radius trigger. This becomes:
function OnTriggerEnter(other:Collider)

{

   var spawn:SpawnPoint = other.GetComponent(SpawnPoint);

   

   if(spawn)

      spawn.DoSpawn();

}

Create a gigantic trigger as a child of your player object, place this script on it, and voila! To prevent collision with your physical gameplay objects, your spawn colliders should also be triggers (yes, triggers send OnTrigger* messages when other triggers enter and exit).
Warning: Trigger colliders do respond to raycasts. You should make sure your triggers are set to the Ignore Raycasts layer if your game logic uses raycasts (or are otherwise ignored by your raycast LayerMasks).

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-1-26 23:26:54 |只看该作者
高雅的人,看背影就知道;奋进的人,听脚步就知道;和善的人,看笑脸就知道;自信的人,看眼神就知道;吉祥的人,看您就知道。祝新年快乐!
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-3-3 23:29:43 |只看该作者
发了那么多,我都不知道该用哪个给你回帖了,呵呵
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2012-3-11 23:19:49 |只看该作者
路过……
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-4-4 23:32:53 |只看该作者
不会吧,太恐怖了
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

6#
发表于 2012-6-18 23:22:26 |只看该作者
加精、加亮滴铁子,尤其要多丁页丁页
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2012-7-5 23:22:18 |只看该作者
不会吧,太恐怖了
回复

使用道具 举报

643

主题

1

听众

9937

积分

高级设计师

Rank: 6Rank: 6

纳金币
9935
精华
1

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

8#
发表于 2012-7-6 15:12:25 |只看该作者
好好好好好好好好好好好好好好好
回复

使用道具 举报

643

主题

1

听众

9937

积分

高级设计师

Rank: 6Rank: 6

纳金币
9935
精华
1

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

9#
发表于 2012-7-6 15:12:33 |只看该作者
好好好好好好好好好好好好好好好
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

10#
发表于 2012-7-9 23:27:22 |只看该作者
很有心,部分已收录自用,谢谢
回复

使用道具 举报

12 第1页 | 共2 页下一页
返回列表 发新帖
您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-29 13:36 , Processed in 1.037344 second(s), 31 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部