问题描述
我正在Unity3d中使用新的2d工具,并且我有一个附带2d多边形对撞机的游戏对象.我只想了解如何获取此对象的宽度!为什么这么难?对撞机或游戏对象都具有"bounds"或"size"属性.即使他们这样做,我如何从vector3对象推断宽度?根据我对vector3对象的了解,它们给出了距场景起点的长度(大小).那么,如何使用距场景原点的距离来确定游戏对象的宽度?任何帮助将不胜感激.
I'm using the new 2d tools in Unity3d, and I have a game object with a 2d polygonal collider attached to it. I just want to understand how to get the width of this object! Why is this so difficult? The collider nor the game object have anything that has a "bounds" or "size" property.Even if they did, however, how would I extrapolate width from a vector3 object? From what I understand about vector3 objects, they give a length (magnitude) from the origin of the scene. So how could I use distance from the scene's origin to determine with width of my game object? Any help would be greatly appreciated.
推荐答案
根据此帖子,看起来2D对撞机确实应该具有缺少的bounds变量,并且它将在将来的版本中提供.
According to this post, looks like 2D colliders indeed should have the missing bounds variable, and it will be made available in a future release.
因此,现在看来您需要解决一个变通方法.一种可能是向您的GameObject添加一个Sprite Renderer组件,向其中添加一个虚拟图像并将其设置为禁用,从而使其永远不可见.然后,您可以使用gameObject.renderer.bounds
来获取精灵边界.
So looks like for now you'll have to make do with a workaround. One could be to add a Sprite Renderer component to your GameObject, add a dummy image to it and set it disabled so it's never visible. Then you can use gameObject.renderer.bounds
to get the sprite bounds.
这篇关于以统一的3d获取2d对象的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!