Rotating 2D square in 3D space around the Y axis

The core of what I’m attempting to do is create a 2D square (points across X and Y axis), and rotate it around the Y axis. So only their X and Z values should be changing, their Y value should be stationary.

Here’s my attempt:

    private void CalculateSquarePosition(Vector3 pos, Vector3 rot)
    {
        // Set Dimensions
        float squareX = squareDimensions.x / 2;
        float squareY = squareDimensions.y / 2;

        //calculate position
        Vector3 cornerPos1 = new Vector3(pos.x + squareX, pos.y + squareY, pos.z);
        Vector3 cornerPos2 = new Vector3(pos.x - squareX, pos.y + squareY, pos.z);
        Vector3 cornerPos3 = new Vector3(pos.x + squareX, pos.y - squareY, pos.z);
        Vector3 cornerPos4 = new Vector3(pos.x - squareX, pos.y - squareY, pos.z);

        //calculate rotation
        Vector3 cornerRot1 = CalculateRotation(cornerPos1, rot);
        Vector3 cornerRot2 = CalculateRotation(cornerPos2, rot);
        Vector3 cornerRot3 = CalculateRotation(cornerPos3, rot);
        Vector3 cornerRot4 = CalculateRotation(cornerPos4, rot);

        //final position (this is just a place holder, but don't i need to do something here?)
        Vector3 corner1 = cornerRot1;
        Vector3 corner2 = cornerRot2;
        Vector3 corner3 = cornerRot3;
        Vector3 corner4 = cornerRot4;


        Debug.DrawLine(corner2, corner1, Color.white);
        Debug.DrawLine(corner1, corner3, Color.white);
        Debug.DrawLine(corner3, corner4, Color.white);
        Debug.DrawLine(corner4, corner2, Color.white);
    }

    private Vector3 CalculateRotation(Vector3 position, Vector3 rotation)
    {
        //2d translate rotation = ((x * cos(rot) - y * sin(rot), (x * sin(rot) - y *  cos(rot))

        float rot = rotation.y;
        float posX = position.x;
        float posZ = position.z;

        Vector3 rotatedPosition = new Vector3(posX * Mathf.Cos(rot) - posZ * Mathf.Sin(rot), rot, posX * Mathf.Sin(rot) - posZ * Mathf.Cos(rot));

        return rotatedPosition;
    }

I honestly have no clue what I’m doing wrong, but I’m pretty sure it’s something in here

        private void CalculateSquarePosition(Vector3 pos, Vector3 rot)
        {
            // Set Dimensions
            float squareX = squareDimensions.x / 2;
            float squareY = squareDimensions.y / 2;
    
            //calculate position
            Vector3 corner1 = new Vector3(pos.x + squareX, pos.y + squareY, pos.z);
            Vector3 corner2 = new Vector3(pos.x + squareX, pos.y - squareY, pos.z);
            Vector3 corner3 = new Vector3(pos.x - squareX, pos.y - squareY, pos.z);
            Vector3 corner4 = new Vector3(pos.x - squareX, pos.y + squareY, pos.z);
    
            //calculate rotation
            corner1 = CalculateRotation(corner1, rot);
            corner2 = CalculateRotation(corner2, rot);
            corner3 = CalculateRotation(corner3, rot);
            corner4 = CalculateRotation(corner4, rot);
    
            Debug.DrawLine(corner1, corner2, Color.white);
            Debug.DrawLine(corner2, corner3, Color.white);
            Debug.DrawLine(corner3, corner4, Color.white);
            Debug.DrawLine(corner4, corner1, Color.white);
        }
    
        private Vector3 CalculateRotation(Vector3 position, Vector3 rotation)
        {
            Vector3 rotatedPosition = Quaternion.Euler(rotation)*position;
            return rotatedPosition;
        }

This worked flawlessly, all I had to do afterwards was make the Vector3 entered into the “pos” parameter a local position, and then move it to its global position at that “final position” spot (I don’t want it following the position of the GO its attached to)

I greatly appreciate the help, and if it wouldn’t be a bother, could you provide a link to something that would help me better understand quaternion rotations? I’ve tried looking myself, however I’m never able to wrap my head around any of it

Don’t worry about understanding quaternions. Many people use cars everyday and yet they don’t have a clue on how they work. They just know how to use them.

Well just know that if you multiply a vector by a quaternion then the vector will be rotated.

Personally I went from initially finding quaternions very puzzling to eventually understanding how they worked. But then I later realized I didn’t really understand how they worked.

Maybe one day I’ll understand how they work again. But then maybe I’ll only think I know how they work…

I’m now at the stage where I just use them and abuse them. If a thing rotates when I want it to rotate then I’m happy…

Thank you for replying, I truly appreciate the advice and expect to keep it in mind going forward. I suppose it makes sense considering that’s how I’ve come to understand other concepts while learning unity. Exposure seems to allow for better understanding than memorizing rules, I often find

To be clear though, this is nothing to do with physics, it’s just using Transforms so I’ll move this thread to scripting. Physics is when you use the physics systems!