简体   繁体   English

统一。 从预制件实例化的 object 的“拉伸”

[英]Unity. "Stretching" of an object instantiated from prefab

I am trying to spawn obstacles on the road.我正试图在路上制造障碍。 To do so, I generate a road by spawning its parts, and each part itself spawns several obstacles in bounds of themselves.为此,我通过生成道路的各个部分来生成一条道路,每个部分本身都会在其自身范围内生成多个障碍物。 But some prefabs after being instantiated got strange "stretching" effects.但是一些预制件在实例化后得到了奇怪的“拉伸”效果。 I don't know how to explain it, so I recorded a small video link .不知道怎么解释,就录了个小视频链接 Also, if I spawn that same object by "drag n drop" to the scene, this bug never appears.此外,如果我通过“拖放”到场景来生成相同的 object,则此错误永远不会出现。

This how I spawn obstacles:这就是我产生障碍的方式:

            Vector3 size = GetComponent<Renderer>().bounds.size;
        Vector3 pos = new Vector3(Random.Range(-0.3f*size.x,0.3f*size.x), 30, Random.Range(-0.3f*size.z,0.3f*size.z));
        Debug.Log(pos + transform.position +"");

        GameObject newBottle = Instantiate(minus_prefabs[Random.Range(0, minus_prefabs.Length)], new Vector3(transform.position.x+pos.x,transform.position.y + pos.y,transform.position.z+pos.z), Quaternion.Euler(0, 0, 0));
        newBottle.transform.SetParent(transform);

This happens because the parent GameObject of the model has diffrent sizes on X, Y and Z. This results in some weird stretching.发生这种情况是因为 model 的父游戏对象在 X、Y 和 Z 上具有不同的大小。这会导致一些奇怪的拉伸。 Try going in your prefab and set the scale of your main GameObject to X:1 Y:1 Z:1 for example.尝试进入您的预制件并将主要游戏对象的比例设置为X:1 Y:1 Z:1例如。

Also see: https://www.unity3dtips.com/how-to-fix-objects-stretching-when-rotated/#:~:text=Cause%20of%20the%20object%20stretching,0.01%2C%200.002%2C%200.004%E2%80%9D .另见: https://www.unity3dtips.com/how-to-fix-objects-stretching-when-rotated/#:~:text=Cause%20of%20the%20object%20stretching,0.01%2C%200.002%2C %200.004%E2%80%9D

This also happens to me all the Time.这也一直发生在我身上。 Thankfully its easy to fix!谢天谢地,它很容易修复!

EDIT: When you instantiate the prefab, also make sure it has the same sizes on every axis.编辑:当您实例化预制件时,还要确保它在每个轴上具有相同的尺寸。

As a general rule, whenever possible, don't use any Scale other than 1,1,1 unless you need to and it should be on a leaf node of the hierarchy or prefab not a parent node.作为一般规则,只要有可能,不要使用 1,1,1 以外的任何 Scale,除非您需要,并且它应该位于层次结构的叶节点或预制件上,而不是父节点。 It will make things go much smoother.这将使事情 go 更加顺利。 If you need to change the size of a mesh (because the noob modeler didn't know how to follow the life-sized scaling metrics in their modeling program, or you just want it smaller or larger), you can do that in the Import settings on the FBX.如果您需要更改网格的大小(因为新手建模者不知道如何在他们的建模程序中遵循真人大小的缩放指标,或者您只是想让它变小或变大),您可以在导入FBX 上的设置。

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

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