`

[Unity] Ray 和 tk2dSprite的一些问题

c# 
阅读更多
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即可。官方作者的解释

引用
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.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics