custom mesh object orientation is not correct using

by Muhammad Faizan Khan   Last Updated June 12, 2019 11:13 AM

I am making a custom mesh with the help of Traingulator script. At runtime, user can click and and generate a point then mesh creation start. Here is the way i am getting user input.

void Update()
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit))
        {
            if (Input.GetMouseButtonDown(0))
            {
                GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                sphere.transform.position = hit.point;
                sphere.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
                positions.Add(sphere.transform.position);
                positionsVector2.Add(new Vector2(sphere.transform.position.x, sphere.transform.position.z));

                traingulatorTester.MeshMaker(positionsVector2.ToArray());
            }
        }

    }

And here is the mesh maker function that calling tringulator

 public void MeshMaker(Vector2[] vertices2D)
    {
        // Use the triangulator to get indices for creating triangles
        Triangulator tr = new Triangulator(vertices2D);
        int[] indices = tr.Triangulate();

        // Create the Vector3 vertices
        Vector3[] vertices = new Vector3[vertices2D.Length];
        for (int i = 0; i < vertices.Length; i++)
        {
            vertices[i] = new Vector3(vertices2D[i].x, vertices2D[i].y, 0);
        }

        // Create the mesh
        Mesh msh = new Mesh();
        msh.vertices = vertices;
        msh.triangles = indices;
        msh.RecalculateNormals();
        msh.RecalculateBounds();
        if (gameObject.GetComponent<MeshRenderer>() == null)
        {
            // Set up game object with mesh;
            gameObject.AddComponent(typeof(MeshRenderer));
            filter = gameObject.AddComponent(typeof(MeshFilter)) as MeshFilter;
        }
        filter.mesh = msh;

    }

Now the problem is the rotation of the custom mesh object is not correct look at this. enter image description here

Now I have manually set the rotation. enter image description here

Tags : unity c# mesh


Answers 1


Triangles, normals and other things seem fine in the images, so it must be a problem with vertices. you placed Z axis of spheres in Y axis of positionsVector2 so you have to put Y axis of positionsVector2 into Z axis of new vectors.

vertices[i] = new Vector3(vertices2D[i].x, 0, vertices2D[i].y);
Mohammad Zamanian
Mohammad Zamanian
June 12, 2019 11:07 AM

Related Questions


Updated November 27, 2018 11:13 AM

Updated March 27, 2017 02:13 AM

Updated December 03, 2018 09:13 AM

Updated April 22, 2015 21:05 PM

Updated April 27, 2015 21:05 PM