[英]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;
}
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
允许您将可选参数worldPositionStays
为false
,这可能是您想要做的。 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.