Taking transform rotation into account on calculation

Hello! There is a grid system in the game I'm building. The grid itself is meant to placed in the center of a box collider. My code as it stands right now works flawlessly when the Y rotation of an object is 0 (or 180). However, once the object gets rotated on its Y axis things start working incorrectly (See image for example)

9674669--1378634--Example Pic.jpg

I'm not sure how to work it into the code I have now to use the transforms rotation to build the grid. My gut tells me I need to be using something along the lines of transform.right or something similar. Relevant code is below.

*// Get starting point for grid
private Vector3 GetOriginPosition(int width, int length, float cellSizeX, float cellSizeY)
float excessSpaceLengthX = _boxCollider.size.x - (width * cellSizeX);
float excessSpaceLengthZ = _boxCollider.size.z - (length * cellSizeY);

Vector3 currentPosition = _boxCollider.center + new Vector3(-_boxCollider.size.x, -_boxCollider.size.y, _boxCollider.size.z) * 0.5f;

currentPosition.x += excessSpaceLengthX * 0.5f;
currentPosition.z += excessSpaceLengthZ * 0.5f;

Vector3 transformedPosition = transform.TransformPoint(currentPosition);

_originPosition = transformedPosition;
return transformedPosition;


// Drawing the debug lines
for (int x = 0; x < _gridArray.GetLength(0); x++)
for (int y = 0; y < _gridArray.GetLength(1); y++)
Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f);
Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f);
Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f);
Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f);

// Get the position of a grid cell by grid coordinates
private Vector3 GetWorldPosition(int x, int y)
Vector3 position = new(x, 0f, y);
position.x *= _cellSizeX;
position.z *= _cellSizeY;

 return position + _originPosition;


Any help or tips would be much appreciated!

Edit: My gut was not right :)

I managed to figure it out!

I traded out the above GetWorldPosition method for this:

private Vector3 GetWorldPosition(int x, int y)
    Vector3 localOriginPosition = _gridHolderTransform.InverseTransformPoint(_originPosition);

    Vector3 gridPosition = new(x, 0f, y);
    gridPosition.x *= _cellSizeX;
    gridPosition.z *= _cellSizeY;

    Vector3 newWorldPosition = _gridHolderTransform.TransformPoint(localOriginPosition + gridPosition);

    return newWorldPosition;

The Results!

9674795--1378649--nice results example.jpg

Feels good, doesn't it? :)

Glad you're sorted. I was gonna suggest another approach:

If you can have a common parent object for all this stuff then you can do all the child object updates using only the .localPosition.

This may or may not be relevant to your problem space given you start from world space input, but there are some cases of "local adjustments" that are amenable to the above.

1 Like

It does feel good indeed!

Alot of the stuff for these mechanics are designed around world position input indeed, so although I could switch to use local space here it'd probably be more of a hassle than it'd be worth to go back and rework everything haha. Thank you though, all suggestions are appreciated! <3

1 Like