3
\$\begingroup\$

I'm using Unity 2018 and its really confusing me. I generate a plane mesh in code and when it comes to positioning (transform.position.x) it on the X axis I can't understand why moving it further to the right the X value decreases (into the negatives) and the X value increases to the left??

The Y axis works as expected, up increases the value (positive), down decreases (negative). The Z axis, away from camera = negative and towards = positive.

(Just creating a basic 3D cube game object using the Unity Create tool, the same issue applies.)

I need to figure if this is how Unity is set up now? Or I'm doing something wrong.. But I'm stuck on this!!

Unity viewport showing X of 0

unity viewport showing X of -188 after moving plane to right

//create mesh code:

Mesh CreateMesh(float width, float height)
    {
        Mesh m = new Mesh();
        m.name = "ScriptedMesh";
        m.vertices = new Vector3[] {
         new Vector3(0, 0, 0.01f),
         new Vector3(width, 0, 0.01f),
         new Vector3(width, height, 0.01f),
         new Vector3(0, height, 0.01f)
     };
        m.uv = new Vector2[] {
         new Vector2 (0, 0),
         new Vector2 (0, 1),
         new Vector2(1, 1),
         new Vector2 (1, 0)
     };
        m.triangles = new int[] { 0, 1, 2, 0, 2, 3 };
        m.RecalculateNormals();

        return m;
    }

//build our first wall mesh obj

                    var wallObj = new GameObject("Wall-" + wallc.WallLengthCM + " (" + wallIndex + ")");
                    var _wallMesh = (MeshFilter)wallObj.AddComponent(typeof(MeshFilter));
                    _wallMesh.mesh = CreateMesh((float)wallc.WallLengthCM, (float)wallc.WallHeightCM);
                    MeshRenderer wallRenderer = wallObj.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
                    wallRenderer.material = ShedWallMat;
var wallMesh = Instantiate(_wallMesh,
                new Vector3 { x = 0, y = ((float)wallc.WallHeightCM * 2 * wallIndex) + vertSpace, z = 0 },
                new Quaternion { w = 0, x = 0, y = 0, z = 0 });
\$\endgroup\$
  • 2
    \$\begingroup\$ Can you show us how you're generating your plane mesh in code? Also, a screenshot of your editor showing the translation gizmo and transform properties in the inspector could be helpful. \$\endgroup\$ – DMGregory Jan 29 at 13:03
  • \$\begingroup\$ The actual code where you make changes to transform.position would also be helpful. \$\endgroup\$ – Ed Marty Jan 29 at 13:33
  • \$\begingroup\$ edited question \$\endgroup\$ – Bananas Jan 29 at 14:11
0
\$\begingroup\$

Unity's coordinate system is a little different than you described. From the perspective of the default camera in a new scene (looking along the z+ axis):

  • X+ = right
  • Y+ = up
  • Z+ = forward / away from the default camera (not negative as described in the question)

In this example you've turned your viewpoint around to look "backward" along the world's negative Z axis, so naturally the positive X axis is turned to point leftward from this perspective.

If you turn your view around the other side, you'll find that your mesh disappears. That's because you've wound your triangles in counter-clockwise order, but Unity's materials treat clockwise triangles as front-facing by default. Simply interchange two indices of each triangle to match this convention.

One last minor issue: an all-zero quaternion does not correspond to a 3D rotation. We typically want our quaternions to be unit length. So you may want {w = 1, x = 0, y = 0, z = 0} instead, also available as the constant Quaternion.identity (I suspect Unity is falling back to this when given all zeroes anyway)

\$\endgroup\$
  • \$\begingroup\$ thanks for your help Greggors! I'll try integrating your suggestions in the morning. But I've just had a quick go - created a new project and new scence. Added a cube GO and when I move it RIGHT along the X axis - it still goes intot the negatives.. ? Is my Unity installation incorrectly set up? I can't seem to find any system settings to adjust though.. I must be missing something simple here! \$\endgroup\$ – Bananas Jan 29 at 19:52
  • \$\begingroup\$ Your scene view might just be looking opposite the direction of the camera GameObject. If I'm not mistaken, scene view perspective is saved as an application preference and preserved between runs. Take a look at the transform gizmo, or the compass in the top-right. You should see the blue arrow pointing into the screen and the red arrow pointing right if your scene view is looking "forward" \$\endgroup\$ – DMGregory Jan 29 at 19:58
  • \$\begingroup\$ You're right mate. Thanks. I'll impliment your suggestions - probably be best for me to create a new project, add a camera and cube and use them as my base guides for the mesh. Its good to know that it's something I've done though - it was such a pain of a way to work otherwise!! \$\endgroup\$ – Bananas Jan 29 at 20:00
  • \$\begingroup\$ You can just orbit your scene view to look in the direction you find most useful for your current task, without remaking the project. \$\endgroup\$ – DMGregory Jan 29 at 20:02

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.