- Home /
I found the problem. Another script was changing the parent's transform.localScale to Vector3.one so it messed up the value during calculation
transform.position is wrong when parent has scale different than Vector3.one
I'm trying to raycast from the children Block, using their transform.position as origin point. When the Block's parent's localScale is Vector3.one, the Block's transform.position is correct. But when I try any different value, 0.8, 3.2, the transform.position is way off.
In the example: the debug text "1 (0.7, 2.0, 0.0)" show the logged transform.position of Block (1). On the scene view, the red line simulate the raycast, the origin of the line is wrong, it's not at the center of the Block how I want it.
I dragged the Block out of the parent to see its world position in the inspector, the real value is different from the transform.position that the code is using
This is the code I used:
Debug.DrawRay(draggingPiece.Blocks[i].transform.position, Vector3.back * 10f, Color.red, 1f);
Debug.Log($"{i} {draggingPiece.Blocks[i].transform.position}");
RaycastHit2D raycast = Physics2D.Raycast(draggingPiece.Blocks[i].transform.position, Vector3.back, 10f, boardLayer);
What's going on here?