I have this method, which just raycasts at a position downwards, and checks what tile is there, if there is one:
public static Tile RaycastTile(Vector3 position)
{
Debug.Log("raycastTile at pos: " + position);
var layerMask = 1 << LayerMask.NameToLayer("Floor");
RaycastHit hit;
Debug.DrawRay(position, new Vector3(0, -10, 0), Color.red, 50);
if (Physics.Raycast(position, new Vector3(0, -1, 0), out hit, 10, layerMask))
{
var tile = hit.collider.gameObject.transform;
Debug.LogWarning(tile.name + " - " + tile.parent.name + " - at pos: " + tile.position);
var coordinates = new Vector2Int(Mathf.RoundToInt(tile.position.x) / 2, Mathf.RoundToInt(tile.position.z) / 2);
return map.GetTile(coordinates);
}
else
{
Debug.LogError("Tile not found under position: " + position);
Debug.Break();
return null;
}
}
Currently it is called for the player
at Start() ("starting at: < player's absolute position >")
and at Update() ("update at pos: < player's absolute position >")
And the map is being generated (tiles created, destroyed, etc) in Awake().
and for some reason this is the output:
And the map generation works like this:
Creates a room at (0, 0) with tiles and all, then it searches for a connection point on the existing map, whom the new room could be connected to. (with some rotation, if it's needed)
(Every tile is 2x2 meters, and they are on a strict grid)
So for some reason calling this method at Start() (with the same position) hits a totally different tile. While in Update() it works correctly.
At first I thought that it's because of Destroy(), because the objects are only destroyed at the end of the frame. But I tried DestroyImmediate() and it didn't help. (And after thinking, I realised that destroying objects at the end of the frame wouldn't cause issues. Or is it?)
Then I had the idea, that maybe Start doesn't wait for Awake because it compute heavy. And the raycast in the Start hits a (yet) non translated newly generated room. So I edited the code so every room is initially created at very far away, which is relatively "infinite far away" from the player: at (9999, 9999). But the issue remained.
EDIT: With (9999, 9999) Sometimes (maybe half of the time?) now it is correctly raycasted even at start, while before it was never correct in start.
EDIT2: With (0, 0) it hits correctly at start as well, but very very rarely.
Do you have any idea what could cause this issue?
Sorry if there isn't enough info, feel free to ask. :)