简体   繁体   English

如何根据 Unity 中的另一个游戏对象调整旋转?

[英]How can I adjust rotation based on another gameObject in Unity?

I'm trying to make a chess game in Augmented Reality.我正在尝试在增强现实中制作国际象棋游戏。 I wrote a script which places chessboard on the Plane in AR.我写了一个脚本,将棋盘放在 AR 的平面上。 Then I created mesh with 64 squares which match chessboard tiles.然后我创建了包含 64 个正方形的网格,这些正方形与棋盘格相匹配。 I have a problem placing mesh to match my chessboard(screenshots).我在放置网格以匹配我的棋盘时遇到问题(屏幕截图)。 I think I should rotate mesh by Y axis, but I wasn't able to do that.我想我应该按 Y 轴旋转网格,但我做不到。

placing chessboard:放置棋盘:

 GameObject placedObject = Instantiate(objectToPlace, placementPose.position, placementPose.rotation * Quaternion.Euler(-90f, 0f, 0f));

script that creates and places mesh:创建和放置网格的脚本:

     private float yAdjust = 0F;
    private Vector3 boardCenter = GameObject.Find("Interaction").GetComponent<TapToPlaceObject>().placementPose.position;
 
private void GenerateSquares(float squareSize)
    {
        adjust = new Vector3(-4 * squareSize, 0, -4 * squareSize) + boardCenter;
   
        squares = new GameObject[8,8];
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                squares[i, j] = CreateSquare(squareSize,i,j);
            }
        }
    }
 
    private GameObject CreateSquare(float squareSize, int i, int j)
    {
        GameObject square = new GameObject(string.Format("{0},{1}", i, j));
        square.transform.parent = transform;
 
        Vector3[] vertices = new Vector3[4];
        vertices[0] = new Vector3(i * squareSize, yAdjust, j * squareSize) + adjust;
        vertices[1] = new Vector3(i * squareSize, yAdjust, (j + 1) * squareSize) + adjust;
        vertices[2] = new Vector3((i + 1) * squareSize, yAdjust, j * squareSize) + adjust;
        vertices[3] = new Vector3((i + 1) * squareSize, yAdjust, (j + 1) * squareSize) + adjust;
 
        int[] triangles = new int[] { 0, 1, 2, 1, 3, 2 };
 
        Mesh mesh = new Mesh();
        square.AddComponent<MeshFilter>().mesh = mesh;
        square.AddComponent<MeshRenderer>().material = squareMaterial;
        //square.transform.rotation = Quaternion.Euler(new Vector3(0, boardRotation.eulerAngles.y, 0));
        //square.transform.rotation = boardRotation;
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        square.AddComponent<BoxCollider>();
 
        return square;
    }

screenshot of my problem我的问题截图

You could probably try and just add another您可能可以尝试添加另一个

* Quaternion.Euler(0f, 45f, 0f)

In general though for your squares there is Transform.SetParent which allows you to pass the optional parameter worldPositionStays as false which is probably what you would want to do.一般来说,虽然对于您的方块,有Transform.SetParent允许您将可选参数worldPositionStaysfalse ,这可能是您想要做的。 By setting通过设置

transform.parent = parent

equals calling等于调用

transform.SetParent(parent);

which equals calling这等于调用

transform.SetParent(parent, true);

so the objects keep their original world space orientation and position.因此对象保持其原始世界空间方向和 position。


However, I would actually rather recommend you already create that board once in edit mode, make the entire board a prefab and now when you spawn the board you only need to take care of placing and rotating one single object and all children will already be their correctly placed and rotated within the board.但是,我实际上更愿意建议您已经在编辑模式下创建该板一次,将整个板设为预制件,现在当您生成板时,您只需要注意放置和旋转一个 object,所有孩子都已经是他们的了在板内正确放置和旋转。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使 GameObject 指向 Unity 中的另一个 GameObject? - How can I make a GameObject point towards another GameObject in Unity? 我需要使用 c# 将一个游戏对象旋转到 Unity 中的另一个游戏对象,但我希望旋转仅在 z - I need to rotate a gameObject towards another gameobject in Unity with c#, but i want the rotation to be only in z 如何使游戏对象在跳跃时旋转,然后使其与另一个游戏对象碰撞,然后平稳返回其原始旋转状态? - How can I make a gameObject rotate while jumping, then once it collides with another gameObject, return smoothly to its original rotation? 如何在Unity中根据速度调整转向角? - How can I adjust steer angle based on speed in Unity? 如何在Unity中反映游戏对象的运动? - How can I mirror a gameobject movement in Unity? Unity5-如何限制游戏对象的旋转 - Unity5 - How to limit the rotation of a GameObject 如何停止 Unity 游戏对象的移动和/或旋转 - How to stop movement and/or rotation of a Unity gameObject Unity如何生成具有旋转速度的GameObject - Unity how to spawn a GameObject with a rotation velocity 如何在 Unity3d 中将 C# 组件从一个游戏对象传递到另一个游戏对象 - how can I pass a C# component from one gameobject to another in Unity3d 如何检测游戏对象何时进入另一个游戏对象的区域? - How can I detect when a gameobject enters the area of another gameobject?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM