1
\$\begingroup\$

I'm not sure if the title is correct. But how does one go about the following:

I imported a mesh created in blender.When i import the object it is at the desired rotation and scale, however, when i look at the properties of the transform the scale is 100 and the rotation is 89.9123xxxxxxx.

Is there any way i can rotate it to the desired rotation, scale it to the desired scale then tell unity that it should update the prefab and set this new rotation as 0,0,0 and the scale as 1,1,1.

Reason behind this, is i am tiling a few prefabs, and the rotations are being used.... after a couple of hundred tiles, there are floating point errors.

\$\endgroup\$
1
\$\begingroup\$

You're probably best off using a script to wrap externally created meshes. This way you can let your script handle everything automatically. I struggled a lot to handle models because of the issue you mentioned. Especially the default rotation caused some trouble.

The basic idea is to make your model a child of an empty GameObject which you instantiate yourself. So you don't drag & drop models in your scene but use a Prefab object with the following script instead:

using System.Collections;
using System.Collections.Generic;

using UnityEngine;

public class Model : MonoBehaviour {

    public string resourceName = "default";

    private static string resourcePrefix = "Models/";
    private GameObject _model = null;

    void Start () {
        var name = resourcePrefix + resourceName;
        var resource = Resources.Load (name);

        if (resource == null) {
            UnityEngine.Debug.LogError ("unable to load '" + name + "'");
            return;
        }

        this._model = GameObject.Instantiate (resource) as GameObject;

        if (this._model == null) {
            UnityEngine.Debug.LogError ("unable to instantiate '" + name + "'");
            return;
        }

        this._model.transform.parent = this.transform;
        this._model.transform.localRotation = Quaternion.Euler (-90f, 180f, 0f);
        this._model.transform.localPosition = Vector3.zero;
    }
}

This is just a basic example but you get the idea. The above rotation Quaternion.Euler (-90f, 180f, 0f) works with Blender so you don't have to create your models upside down and stuff like that.

Having a script take care of this gives you a lot of control. You can easily extend the logic to handle stuff like changing the model at runtime or initializing materials properly instead of using the imported ones with something like:

public List<Material> materials = new List<Material> ();

private void SetupMaterials () {
    if (this._model != null) {
        var renderer = this._model.GetComponent<MeshRenderer> ();
        var meshFilter = this._model.GetComponent<MeshFilter> ();

        if (renderer != null && meshFilter != null) {

            if (meshFilter.mesh.subMeshCount != materials.Count) {
                UnityEngine.Debug.LogWarning ("submesh count != materials.Count");
            }

            renderer.materials = materials.ToArray ();
        }
    }
}

Note: Just make sure to store your models somewhere in Assets/Resources/, otherwise Resources.Load () won't find your data.

Hope this helps!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.