Home > Net >  Unity. "Stretching" of an object instantiated from prefab
Unity. "Stretching" of an object instantiated from prefab

Time:04-11

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.

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);

CodePudding user response:

This happens because the parent GameObject of the model has diffrent sizes on X, Y and Z. This results in some weird stretching. Try going in your prefab and set the scale of your main GameObject to X:1 Y:1 Z:1 for example.

Also see: https://www.unity3dtips.com/how-to-fix-objects-stretching-when-rotated/#:~:text=Cause of the object stretching,0.01, 0.002, 0.004”.

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.

CodePudding user response:

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. It will make things go much smoother. 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.

  • Related