So I have three downloaded models that were free for all extended uses. My game object's mesh filter is changed to one of the three models at random on Start. This is done so that there is some randomness to what the objects look like in my game. The object is also assigned one of three options in an enumerator at random, small, medium, and large. I used a switch, where each case is one of the 3 options. The scale is set to 1,1,1 for small, 2,2,2 for medium, and 4,4,4 for large.
Everything works perfectly so that there different sizes of the object, and even several instances of the object that are the same size can be different models. The only problem is that one of the three models is 1.5 times smaller than the other two. The fix seemed easy enough to do with code, I didn't expect to have a problem, but I tried all kinds of things, all of which seemed like they would work but nothing did. I decided that the easiest fix would be to just import the fbx file into Maya, and export it back into Unity the proper size, but for some reason Maya didn't recognize the file. I downloaded Blender to try and change it, but it told me the file was an old version and that Blender couldn't read it.
So I can't figure out how to fix the scale of every object assigned mesh1 in code, and I can't fix the size of the model itself. Is there a way to change the model in code, not through scale, but by redefining the size of the model at 1,1,1 scale?
↑▲▲▲MAIN QUESTION▲▲▲↑
If that's not possible, and you'd care to try and succeed where I have failed with the scale, you can look at my script, but it's kind of big for a forum question I think, and I wouldn't expect any one to put that much effort into solving my problem. Here it is though, I'll try to add some comments to make interpreting it easier...
private int meshRandInt;
public float meshRand;
public float sizeRand;
public float sizeMult;
public Mesh boulder1; //this one is too small
public Mesh boulder2;
public Mesh boulder3;
private bool meshLoaded;
private bool sizeChosen;
public AsteroidSize asteroidSize;
public enum AsteroidSize
{
Small,
Medium,
Large
}
void Start () {
meshRand = (Random.Range(0f, 3f));
meshRandInt = Mathf.FloorToInt(meshRand); //Gives a value, 0,1 or 2 for the loadMesh method
sizeRand = (Random.Range(0f,10f));//for choosing which size the object will be
}
void Update ()
{
switch (asteroidSize) //changes the scale of the object based on AsteroidSize(NOT BASED ON //MESH, but boulder1 mesh needs a 2.5 scale base to be the correct size)
{
case AsteroidSize.Small:
sizeMult = 1;
transform.localScale = new Vector3(1, 1, 1) * sizeMult;
break;
case AsteroidSize.Medium:
sizeMult = 2;
transform.localScale = new Vector3(1, 1, 1) * sizeMult;
break;
case AsteroidSize.Large:
sizeMult = 4;
transform.localScale = new Vector3(1, 1, 1) * sizeMult;
break;
}
if (!sizeChosen) //Assigns at random the AsteroidSize
{
if (sizeRand >= 0 && sizeRand <= 5)//0-5 = small %50 chance
{
asteroidSize = AsteroidSize.Small;
sizeChosen = true;
} else if (sizeRand > 5 && sizeRand <= 8) // %30 for medium
{
asteroidSize = AsteroidSize.Medium;
sizeChosen = true;
} else if (sizeRand > 8) //%20 for Large
{
asteroidSize = AsteroidSize.Large;
sizeChosen = true;
}
}
LoadMesh();
}
public void LoadMesh() //Assigns different meshes depending on the value of meshRandInt
{
if (meshRandInt == 0)
{
GetComponent().mesh = boulder1;
if (!meshLoaded)
{
GetComponent().sharedMesh = null;
GetComponent().sharedMesh = GetComponent().mesh;
meshLoaded = true;
}
}
if (meshRandInt == 1)
{
GetComponent().mesh = boulder2;
if (!meshLoaded)
{
GetComponent().sharedMesh = null;
GetComponent().sharedMesh = GetComponent().mesh;
meshLoaded = true;
}
}
if (meshRandInt == 2)
{
GetComponent().mesh = boulder3;
if (!meshLoaded)
{
GetComponent().sharedMesh = null;
GetComponent().sharedMesh = GetComponent().mesh;
meshLoaded = true;
}
}
}
I tried wrapping the transform.local size statements from the switch in an if statement that first checks if the mesh filter is set to boulder1, if it is then it sets the scale to 2.5,2.5,2.5 and multiplies it by the size multiplier, if it isn't boulder1, then it sets the scale to 1,1,1 and multiplies it, but that didn't work. It still seems like it would to me. IDK, but I need to figure this out, because if a large size asteroid with the boulder1 mesh is destroyed, two medium sized asteroids will be instantiated and there will be more mass(in the eyes of the player) than there was before and it won't make sense. Now that I think about it, I could leave out boulder1 and just use the other two, but I'd rather not.
↧