Search Unity

Prevent A Spawned Object From Scaling

Discussion in 'Scripting' started by vcinardo, Jun 29, 2022.

  1. vcinardo

    vcinardo

    Joined:
    Dec 23, 2019
    Posts:
    31
    I have objects that spawn in via script in empty GameObjects. The script sets a spheroid (The World) as the parent of the newly spawned in objects. The world is a big stretched sphere. The objects seem to be following suit, stretching out as well. Is there a way to prevent this. Here is my script on each empty GameObject that spawns stuff:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BackgroundSpawner : MonoBehaviour
    6. {
    7.     //A list of prefabs to choose from. Consist of the object with the death collider and pass collider which awards points.
    8.     public GameObject[] prefabs;
    9.  
    10.     public void Spawn()
    11.     {
    12.         Vector3 vec = new Vector3(transform.position.x, transform.position.y, transform.position.z);
    13.         //GameObject choice = prefabs[Random.Range(0, 5)];
    14.         GameObject prefab = Instantiate(prefabs[Random.Range(0, 5)], vec, transform.rotation, transform.parent);
    15.         //prefab.transform.localScale = choice.transform.localScale;
    16.        
    17.         Ray down = new Ray(transform.position, -Vector3.up);
    18.         RaycastHit hit;
    19.         if (Physics.Raycast(down, out hit))
    20.         {
    21.             prefab.transform.RotateAround(transform.position, transform.up, Random.Range(0.0f, 360.0f));
    22.             prefab.transform.position = prefab.transform.position - new Vector3(0.0f, hit.distance, 0.0f);
    23.         }
    24.     }
    25. }
    26.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    The scaling is probably happening due to the fourth "parent" argument to Instantiate in line 14 above.

    Prove / disprove that theory, and if it holds, try removing the argument, then do the parenting yourself with .SetParent(), using an appropriate true / false for the second optional argument to SetParent(). See docs.
     
  3. vcinardo

    vcinardo

    Joined:
    Dec 23, 2019
    Posts:
    31
    Works, appreciate that!
     
    Kurt-Dekker likes this.
  4. vcinardo

    vcinardo

    Joined:
    Dec 23, 2019
    Posts:
    31
    Hi I'm sorry, false alarm it's still stretching, it just so happened it spawned in the thinner trees so it did not look stretched. Any suggestions?
     
  5. ferlinnoise

    ferlinnoise

    Joined:
    Jul 23, 2021
    Posts:
    7
    Try spawning it first without a parent and then do transform.parent = bigStretchedSphereTransform.
    It should do.
     
  6. vcinardo

    vcinardo

    Joined:
    Dec 23, 2019
    Posts:
    31
    Here is my new code I can't seem to figure it out.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Spawner : MonoBehaviour
    6. {
    7.     //A list of prefabs to choose from. Consist of the object with the death collider and pass collider which awards points.
    8.     public GameObject[] prefabs;
    9.     public GameObject earth;
    10.  
    11.     public void Spawn()
    12.     {
    13.         Vector3 vec = new Vector3(transform.position.x, transform.position.y, transform.position.z);
    14.         GameObject prefab = Instantiate(prefabs[Random.Range(0, prefabs.Length)], vec, transform.rotation);
    15.         prefab.transform.SetParent(earth.transform, true);
    16.         Ray down = new Ray(transform.position, -Vector3.up);
    17.         RaycastHit hit;
    18.         if (Physics.Raycast(down, out hit))
    19.         {
    20.             transform.Rotate(new Vector3(0.0f, Random.Range(0.0f,360.0f), 0.0f));
    21.             prefab.transform.position = prefab.transform.position - new Vector3(0.0f, hit.distance, 0.0f);
    22.         }
    23.     }
    24. }
    25.  
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Your issue is that you have a parent object that is not scaled uniformly.

    Rule of thumb is that any object that has children should have uniform scaling, else you will deal with stretching/skewing issues like this. To fix it, refactor your hierarchy such that the parent object is not scaled. You can move any renderers/colliders etc that need to be scaled to a separate child/sibling GameObject instead.