[Unity] Ray 和 tk2dSprite的一些问题

Ray实际上就是一个Vector3,我们在调试的时候可以使用Debug.DrawRay()这个函数。注意这个函数可以设置ray的颜色,这样方便我们直观的看出来,这个debug的只能在scene的时候显示,因此需要我们开两个小的窗口,一个显示scene,一个显示game,而我们在game窗口进行操作的时候,scene那里就可以显示ray的位置了。简单的示例如下:
Color color = hit ? Color.green : Color.red;    
Debug.DrawRay (transform.position, Vector3.forward * 10, color);

今天遇到的一个奇怪的bug,就是我创建了一个tk2dSprite,但即使我加入了box collider之后,还是无法产生raycast的hit事件,之后查阅很多网站,终于发现原来如果一个sprite是在sprite collection之中的话,那么这个sprite collection会定义这个sprite的collider的类型,如果是none,那么即使我们在外面定义box collider也是没有用处的,解决方案一种是在sprite collection中将类型变成user defined,另外就是在collection中定义好,然后外面不需要添加自己的collider即可。[url=http://forum.unity3d.com/threads/93287-2D-Toolkit-2D-in-Unity-made-simple-RELEASED?p=768754&viewfull=1#post768754]官方作者的解释[/url]:

[quote]The reason this is happening, is that you are trying to create your own collider, while the setting in the SpriteCollection for the particular sprite is "None".

When an object already has a box collider and the sprite is set to "None" collision, 2d toolkit will set its bounds to 0,0,0 and origin z to -100000. This is primarily for animation, for instance, if 5 frames of animation have a collider and 3 dont.

2 ways to solve this -

1. if you need to mange the collider yourself, set it to "Unset" in the sprite collection. This means, the sprite will not try to manage the collider, and you can do what you wish with it.

2. Set it to BoxTrimmed in the Sprite collection. You won't need to manage the collider any more at this point, and it will automatically fit the visible part of the texture.

Hope this helps.
[/quote]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值