Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by yomope · Jan 10, 2014 at 06:14 PM · scalescalingfit

How to scale an object clone to fit a cube

Hi everyone, I have in my scene an empty gameobject that spawn random modele of different scale.I want the clone to fit in the empty gameobject. i try to scale the object in blender but as i have a huge list of object it's a real pain in the *ss to make them one at a time. Can someone help? thanx! much love and respect to this amazing community :)

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Jan 10, 2014 at 06:24 PM 0
Share

I'm having trouble visualizing the 'right' solution for your problem:

  • Are you scaling on all axes (non-uniform) so the object fits, or are you uniformly scaling so that the greatest side fits?

  • Do you have to take into account rotation?

  • Are you looking for a runtime solution or an edit time solution?

  • Would a hands-on editor script work where you resize the object and then apply the script so that the result has scale (1,1,1)?

avatar image yomope · Jan 10, 2014 at 08:07 PM 0
Share

yes i want a uniform scaling and we dont need to take care of rotation. as english is not my native language and as i am a noob, im not sure of the difference between runtime and edit time solution can you explain to me? I spawn object from asset list to spare memory and they are use in other scene. Will editor script make a mess in my other scene? finally, a lot of the model has a rather large amount of poly so i can't load all of them at the same time.

thank a lot for your interest in my post :)

avatar image helloiam · Jan 10, 2014 at 08:49 PM 0
Share

Can you clearify a couple things for me so you want to clone objects in the empty gameobject which you have in the game how do you mean by fitting empty gameobject are invisble right?

avatar image helloiam · Jan 10, 2014 at 09:05 PM 0
Share

Otherwise just do:

 using UnityEngine;
 using System.Collections;
 
 public class Resizer : $$anonymous$$onoBehaviour {
 
     public GameObject WhatToSpawn;
     public GameObject whereToSpawn;
     public int how$$anonymous$$anyTimes = 10;
 
     void Start ()
     {
         for (int i = how$$anonymous$$anyTimes; i > 0; i--) 
         {
             GameObject clonePrefab = Instantiate(WhatToSpawn, whereToSpawn.transform.position, Quaternion.identity) as GameObject;
             clonePrefab.transform.localScale = new Vector3(clonePrefab.transform.localScale.x / i,
                                                  clonePrefab.transform.localScale.y / i, clonePrefab.transform.localScale.y / i);
         }
     }
 }
 

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by robertbu · Jan 10, 2014 at 09:26 PM

I'm still unclear about the nature of an ideal solution. Note that an empty game object does not have a size, so I'm unsure what you mean by "fit in the empty gameobject". So I'll toss something out there and you can give feedback as a way to figure out what you need. The following script would need to be attached to items that you clone. The variable 'emptySize' is the size you want to fit into and needs to be set in the Inspector. For example, if you want to fit into a space of (1x1x1), then you would set this value to 1.0.

 #pragma strict
 
 var emptySize = 1.0;
 
 function Start() {
     var mf = GetComponent(MeshFilter);
     
     if (mf != null) {
         var bounds = mf.mesh.bounds;
         
         var max = bounds.extents.x;
         if (max < bounds.extents.y) 
             max = bounds.extents.y;
         if (max < bounds.extents.z)
             max = bounds.extents.z;
             
         var scale = (emptySize * 0.5) / max;
         transform.localScale = Vector3(scale, scale, scale);
     }
 }

This is a runtime solution. That is at the time each object is Instantiated while the game is running, it resizes the objects.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mrCrush · Feb 18, 2018 at 05:43 PM 0
Share

Thank you, man! It helped a lot.

avatar image
0

Answer by yomope · Jan 11, 2014 at 02:10 PM

thanks a lot for your answers!! i've used robertbu's code because i can read and (almost)understand javascript. it work in a different way than the one i tought and it improve the scene. so thank a lot i manage to use it in a different way by attaching the script to the clone when it spawn.but (because there is alway a but) i' had a script that make the object turn around itself and i can't add the 2 component at the same time. i think i am pretty close but i cant manage. here is the main script in the scene

  var spawnlist : GameObject[];
  var oscChannel : int;
  private var actu: int= 0;
  private var toload: int= 0;
  private var rotactu: int= 90;
  var tempo: int= 0;
  private var obj : GameObject;
  function Start () {
  
      Instantiate (spawnlist[toload], transform.position, transform.rotation).AddComponent("holo_rotate").transform.tag = "holo";
 
  }
 
  function Update () {
 
     var n = OSCReceiver.messages[oscChannel];
     actu = actu+1;
     toload = Random.Range(0, spawnlist.Length);
     obj = GameObject.FindWithTag("holo");
         var rotate = false;
     
         
         
         
         if (Input.GetKeyDown(KeyCode.A)) {
             
             //Destroy (GameObject.FindWithTag("holo"));
             Destroy(obj);
             Instantiate (spawnlist[toload], transform.position, transform.rotation).AddComponent("holo_rotate").transform.tag = "holo";
             GameObject.FindWithTag("holo").AddComponent("scaletofit");
             
         }
         
     
 
 
     
 }


when i try to add component at the same in the if loop only the first component is added. and when i add it outside my clone get a bunch of it since its the update function. i try another if loop but it hasn't work.

the final look of the scene is various object rotating in the middle of the screen.(wich seem pretty easy at start)

can you give me a hint?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Jan 11, 2014 at 02:46 PM 0
Share

You can do it this way:

 var go = Instantiate (spawnlist[toload], transform.position, transform.rotation) as GameObject;
 
 go.tag = "holo";
 go.AddComponent(holo_rotate);
 go.AddComponent(scaletofit);
      

You should do #pragma strict at the top of the file. Also don't use the string version of AddComponent().

avatar image yomope · Jan 11, 2014 at 07:48 PM 0
Share

it work great but it seems that object with pultiple part dont get the resize. Do i need to add component to the child ? i will try to find out but if you have a clue, i take it :) thank a lot for what you've done for me :)

I still have 2 questions left (for my education): - why #pragma script is important ? - Why why shouldn't i use the string version?

virtual beer for you my friend.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get an object to fill 3/4th of the screen. 0 Answers

smooth scailing (gui title image) 0 Answers

How to scale a GUI (Lagacy) grid? 1 Answer

Unity Scaling an object from scripts 1 Answer

How to preserve size of children when changing scale of parent 5 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges