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

Viewed 86

I'm trying to make a chess game in Augmented Reality. I wrote a script which places chessboard on the Plane in AR. Then I created mesh with 64 squares which match chessboard tiles. 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.

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

1 Answers

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. 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.


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.

Related