- Home /
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 :)
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)?
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 :)
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?
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);
}
}
}
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.
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?
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().
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.