Create a grid with the same normal as raycast normal using quaternion

I am trying to create a grid plane which will have the normal from a raycast, and it Should be constrained inside a box. I have made the grid plan, but i have problems with adjusting the grid to fit inside the box. I can create the grid on a specific point, but if i try to offset the grid then i will encounter som weird rotations or scaling.

If i can at least control the offsets on the grid then i can move it to somewhat fit inside a box bounds.
I am using ECS but if you have a solution in standard Monobehaviour, then that might work for helping me understand how to solve this

My result

What i want to achieve

My current attempt in code

``````    private void GridCreation(GameObject originGO, GameObject testObject1, GameObject testObject2, float testValue1, float testValue2, float angle1, float angle2, float offsetX, float offsetY)
{
float3 direction1 = new float3(testObject1.transform.position.x, testObject1.transform.position.y, testObject1.transform.position.z);
float3 origin = new float3(originGO.transform.position.x, originGO.transform.position.y, originGO.transform.position.z);

Debug.DrawLine(origin, direction1,Color.black);

float degreesToRad = math.PI / 180f;

float3 newOriginDir = direction1 * testValue1;

float3 offestFloat3X = math.cross(newOriginDir, math.forward(rotation1) * offsetX);
float3 offestFloat3Y = math.cross(newOriginDir, math.forward(rotation2) * offsetY);

float3 firstCourner1 = newOriginDir + (offestFloat3X + offestFloat3Y);
float3 firstCourner2 = firstCourner1 + (math.cross(firstCourner1, math.forward(rotation1)) * testValue2);
float3 firstCourner3 = firstCourner2 + (math.cross(firstCourner1, math.forward(rotation2)) * testValue2);
float3 firstCourner4 = firstCourner1 + (math.cross(firstCourner1, math.forward(rotation2)) * testValue2);

List<float3> positionList = new List<float3>();
List<float3> positionListY = new List<float3>();

bool firstXPos = true;

int xLenght = 5;
int yLenght = 5;

//Create several quads in a row
for (int yI = 0; yI < yLenght; yI++)
{
firstXPos = true;
if (firstXPos)
{

float3 rowCourner1 = positionListY[1];
float3 rowCourner2 = positionListY[1] + (math.cross(firstCourner1, math.forward(rotation1)) * testValue2);
float3 rowCourner3 = rowCourner2 + (math.cross(firstCourner1, math.forward(rotation2)) * testValue2);
float3 rowCourner4 = positionListY[1] + (math.cross(firstCourner1, math.forward(rotation2)) * testValue2);

positionList.Clear();

firstXPos = false;

}

for (int xI = 0; xI < xLenght; xI++)
{

if (xI == 0)
{
newQuadCourner2 = positionList[0] + (math.cross(firstCourner1, math.forward(rotation1)) * testValue2);
newQuadCourner4 = positionList[0] + (math.cross(firstCourner1, math.forward(rotation2)) * testValue2);

}
else
{
newQuadCourner2 = positionList[3] + (math.cross(firstCourner1, math.forward(rotation1)) * testValue2);
newQuadCourner4 = positionList[3] + (math.cross(firstCourner1, math.forward(rotation2)) * testValue2);

}

positionList.Clear();

if (xI == 0)
{
positionListY.Clear();

}

}

}

//float3 testThirdCourner3 = (newOriginDir + (math.cross(newOriginDir, math.forward(rotation3)) * testValue2)) + (math.normalize(math.forward(rotation2)));

//Test position
float3 newDirection = newOriginDir + (math.cross(newOriginDir, math.forward(rotation1)) * testValue2);

//originGO.transform.position = new Vector3(newOriginDir.x, newOriginDir.y, newOriginDir.z);
testObject2.transform.position = new Vector3(newDirection.x, newDirection.y, newDirection.z);

}
``````

Sort of solved it, for anyone that is interested in this problem here is the code.

`````` private void GridCreation(float angle1, float offsetX, float offsetY, int gridCount, float quadSize, List<float3x4> cubeList)
{
float3 targetPos = new float3(testObject1.transform.position.x, testObject1.transform.position.y, testObject1.transform.position.z);
float3 origin = new float3(originGO.transform.position.x, originGO.transform.position.y, originGO.transform.position.z);

float3 cubeCenter = (cubeList[0].c0 + cubeList[0].c1 + cubeList[0].c2 + cubeList[0].c3 + cubeList[1].c0 + cubeList[1].c1 + cubeList[1].c2 + cubeList[0].c3) / 8;

float distanceXToCenter = origin.x + cubeCenter.x;
float distanceZToCenter = origin.z + cubeCenter.z;

offsetX = distanceXToCenter;
offsetY = distanceZToCenter;

float3 offsetPosition = origin;

float degreesToRad = math.PI / 180f;

float3 newOriginDir = math.normalize(targetPos);

offsetPosition = offsetPosition - newOriginDir;

float3 firstCourner1 = newOriginDir;
float3 firstCourner2 = (firstCourner1 - (math.cross(firstCourner1, math.forward(rotation1)) * offsetX));
float3 firstCourner3 = (firstCourner2 - (math.cross(firstCourner1, math.forward(rotation2)) * offsetY));
float3 firstCourner4 = (firstCourner1 - (math.cross(firstCourner1, math.forward(rotation2)) * offsetY));

CustomTools.CustomGeometryTools customGeometryTools = new CustomTools.CustomGeometryTools();
List<float3> positionList = new List<float3>();
List<float3> positionListY = new List<float3>();
List<float3x4> totalGridList = new List<float3x4>();
List<float3x4> finalGridList = new List<float3x4>();

bool firstXPos = true;

//Create several quads in a row
for (int yI = 0; yI < gridCount; yI++)
{
firstXPos = true;
if (firstXPos)
{

float3 rowCourner1 = positionListY[1];
float3 rowCourner2 = positionListY[1] + (math.cross(firstCourner1, math.forward(rotation1)) * quadSize);
float3 rowCourner3 = rowCourner2 + (math.cross(firstCourner1, math.forward(rotation2)) * quadSize);
float3 rowCourner4 = positionListY[1] + (math.cross(firstCourner1, math.forward(rotation2)) * quadSize);

totalGridList.Add(new float3x4( rowCourner1, rowCourner2, rowCourner3, rowCourner4));

positionList.Clear();

firstXPos = false;

}

for (int xI = 0; xI < gridCount; xI++)
{

if (xI == 0)
{

}
else
{

}

positionList.Clear();

if (xI == 0)
{
positionListY.Clear();

}

}

}

for (int i = 0; i < totalGridList.Count; i++)
{
totalGridList <em>= new float3x4(totalGridList<em>.c0 + offsetPosition, totalGridList<em>.c1 + offsetPosition, totalGridList_.c2 + offsetPosition, totalGridList*.c3 + offsetPosition);*_</em></em></em>
``````

if (customGeometryTools.InsideBox3D(cubeList[1], cubeList[0], totalGridList*.c0) &&*
customGeometryTools.InsideBox3D(cubeList[1], cubeList[0], totalGridList*.c1) &&*
customGeometryTools.InsideBox3D(cubeList[1], cubeList[0], totalGridList*.c2) &&*
customGeometryTools.InsideBox3D(cubeList[1], cubeList[0], totalGridList*.c3))*
{