简体   繁体   English

如何将游戏对象设置为统一的精确长度

[英]How to set a gameobject to an exact length in unity

In Unity I have a float that represents a distance 在Unity中,我有一个代表距离的浮动

float distance = Vector3.Distance(start.position,end.position);

I want to use that float to change the size of an object along its Z axis. 我想使用该浮动来改变对象沿Z轴的大小。 I tried: 我试过了:

gameobject.transform.localScale.z = distance;

However, this just changes the scale, I need to change the game object to an exact length. 但是,这只是改变了比例,我需要将游戏对象更改为精确的长度。 Is there a straightforward way to achieve this? 有没有直接的方法来实现这一目标?

I created a workaround function to convert a specific length into a scale (for anyone who may find this question in the future). 我创建了一个解决方法函数,将特定长度转换为比例(对于将来可能会发现此问题的任何人)。 Game objects can only have their size changed though scale, but their exact size can be found through the renderer. 游戏对象只能通过缩放更改其大小,但可以通过渲染器找到它们的确切大小。 using a little interpolation you can convert this into a new scale to resize your object to an exact size. 使用一点插值,您可以将其转换为新的比例,以将对象的大小调整为精确的大小。

public void newScale(GameObject theGameObject, float newSize) {

    float size = theGameObject.GetComponent<Renderer> ().bounds.size.y;

    Vector3 rescale = theGameObject.transform.localScale;

    rescale.y = newSize * rescale.y / size;

    theGameObject.transform.localScale = rescale;

}

Unity has no thing like "Length" for any object. 对于任何对象,Unity都没有“长度”之类的东西。 At the scale of (1,1,1) unity considers that every object is of one unit. 在(1,1,1)的范围内,统一认为每个物体都是一个单位。 meaning if you want your object to be 3 units long along z axis you change its scale to (1,1,3) and similarly for other axis. 意思是如果您希望对象沿z轴长3个单位,则将其比例更改为(1,1,3),对于其他轴也是如此。 thats why it is said that you need to take special care while designing assets for unity as they all need to made according to unity defined scale. 这就是为什么据说你需要特别小心,同时为统一设计资产,因为他们都需要按照统一的规模来制作。

This solution worked better for me: 这个解决方案对我来说更好:

public void convertDistanceToScale(GameObject go, float distance){
     Vector2 size = go.transform.lossyScale;
     size.x = distance;
     size.x = (size.x * go.transform.localScale.x) / go.transform.lossyScale.x;
     size.y = go.transform.localScale.y;
     go.transform.localScale = size;
}

This works for all objects I've tested it on, including child objects and rotated objects. 这适用于我测试过的所有对象,包括子对象和旋转对象。

image here 图像在这里

in my case , this object Radius 243.638 at 3dsmax . 在我的例子中,这个对象Radius 243.638 at 3dsmax。

image here 图像在这里

but this re-scale cause this is child object . 但这种重新缩放导致这是子对象。 and want fix the width as parent object variable . 并希望将宽度固定为父对象变量。

image here 图像在这里

    Transform Truck_Area_Range = transform.Find("Circle");
    Mesh mesh = Truck_Area_Range.GetComponent<MeshFilter>().mesh;
    float Mesh_X = mesh.bounds.size.x;
    Debug.Log(Mesh_X); // this same as my 3dsmax Radius * 2
    Vector3 LocalScale = transform.localScale;
    Debug.Log(LocalScale.x); // parent object LocalScale
    float current_size = Mesh_X * LocalScale.x;
    Debug.Log(current_size); // Circle object size in scene
    float distance = 30;
    float newSize = distance / current_size;
    Debug.Log(newSize);  
    Truck_Area_Range.localScale = new Vector3 (newSize, newSize, 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM