纳金网

标题: unity3d中Trigger的使用探讨(一) [打印本页]

作者: 会飞的鱼    时间: 2011-12-14 14:40
标题: unity3d中Trigger的使用探讨(一)
原文标题为: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).


作者: C.R.CAN    时间: 2012-1-26 23:26
高雅的人,看背影就知道;奋进的人,听脚步就知道;和善的人,看笑脸就知道;自信的人,看眼神就知道;吉祥的人,看您就知道。祝新年快乐!

作者: tc    时间: 2012-3-3 23:29
发了那么多,我都不知道该用哪个给你回帖了,呵呵

作者: tc    时间: 2012-3-11 23:19
路过……

作者: tc    时间: 2012-4-4 23:32
不会吧,太恐怖了

作者: C.R.CAN    时间: 2012-6-18 23:22
加精、加亮滴铁子,尤其要多丁页丁页

作者: C.R.CAN    时间: 2012-7-5 23:22
不会吧,太恐怖了

作者: BU Secret    时间: 2012-7-6 15:12
好好好好好好好好好好好好好好好
作者: BU Secret    时间: 2012-7-6 15:12
好好好好好好好好好好好好好好好
作者: 菜刀吻电线    时间: 2012-7-9 23:27
很有心,部分已收录自用,谢谢

作者: 晃晃    时间: 2012-10-5 23:26
我看看就走,你们聊!

作者: 晃晃    时间: 2013-3-12 23:23
我看看就走,你们聊!

作者: C.R.CAN    时间: 2013-3-19 23:21
已阵亡的 蝶 随 风 舞 说过  偶尔按一下 CTRL A 会发现 世界还有另一面

作者: nts    时间: 2013-10-17 14:16
英文的,看得半懂




欢迎光临 纳金网 (http://rs.narkii.com/club/) Powered by Discuz! X2.5