Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question finding the real of my tile's colliders

Discussion in 'Scripting' started by ChaosEaterY, Apr 3, 2024.

  1. ChaosEaterY

    ChaosEaterY

    Joined:
    Jun 3, 2018
    Posts:
    5
    I have an isometric tileset. the tiles in it go out of the bounds of a single isometric unit. the issue is that I want to use the tiles as a grid and because they go out of bounds the bottom part of the tiles that are dirt overlap with the tiles south of them. how do I go about fixing that? its not like my code does not work but it is quite messy

    Code (CSharp):
    1. public Transform Player;
    2. public Tilemap Tilemap;
    3. public GameObject HoveringObject;
    4. public float MoveSpeed = 5f;
    5. public float TargetHeightAboveTile = 0.5f;
    6.  
    7. Vector3 targetPosition;
    8.  
    9. bool isMoving;
    10.  
    11. void Update()
    12. {
    13.     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    14.     RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity);
    15.  
    16.     if (hit.collider != null && hit.collider.GetComponent<Tilemap>() == Tilemap)
    17.     {
    18.         Vector3Int tilePosition = Tilemap.WorldToCell(hit.point);
    19.         Vector3 centerOfTile = Tilemap.GetCellCenterWorld(tilePosition);
    20.  
    21.         centerOfTile.y += Tilemap.cellSize.y / 3.85f;
    22.         centerOfTile.x = Mathf.Clamp(centerOfTile.x, Tilemap.cellBounds.min.x, Tilemap.cellBounds.max.x);
    23.         centerOfTile.y = Mathf.Clamp(centerOfTile.y, Tilemap.cellBounds.min.y, Tilemap.cellBounds.max.y);
    24.  
    25.         if (IsPointWithinBounds(centerOfTile)) HoveringObject.transform.position = centerOfTile;
    26.  
    27.         centerOfTile.y += TargetHeightAboveTile;
    28.  
    29.         if (Input.GetMouseButtonDown(0))
    30.         {
    31.             targetPosition = centerOfTile;
    32.             isMoving = true;
    33.         }
    34.     }
    35.  
    36.     if (isMoving && IsPointWithinBounds(new Vector2(targetPosition.x, targetPosition.y - TargetHeightAboveTile))) MovePlayer();
    37. }
    38.  
    39. void MovePlayer()
    40. {
    41.     Vector3 moveDirection = (targetPosition - Player.position).normalized;
    42.  
    43.     Player.position += moveDirection * MoveSpeed * Time.deltaTime;
    44.  
    45.     if (Vector3.Distance(Player.position, targetPosition) < 0.1f)
    46.     {
    47.         Player.position = targetPosition;
    48.         isMoving = false;
    49.     }
    50. }
    51.  
    52. public Vector2[] vertices;
    53.  
    54. public bool IsPointWithinBounds(Vector2 point)
    55. {
    56.     int numVertices = vertices.Length;
    57.     bool isInside = false;
    58.     int j = numVertices - 1;
    59.  
    60.     for (int i = 0; i < numVertices; i++)
    61.     {
    62.         if ((vertices[i].y < point.y && vertices[j].y >= point.y || vertices[j].y < point.y && vertices[i].y >= point.y) &&
    63.             (vertices[i].x <= point.x || vertices[j].x <= point.x))
    64.         {
    65.             if (vertices[i].x + (point.y - vertices[i].y) / (vertices[j].y - vertices[i].y) * (vertices[j].x - vertices[i].x) < point.x)
    66.             {
    67.                 isInside = !isInside;
    68.             }
    69.         }
    70.         j = i;
    71.     }
    72.  
    73.     return isInside;
    74. }
    I have a lot of ideas but im not sure how to implement them if at all possible. for example since GetCellCenterWorld() is not right I thought maybe I should find the center of the tile's own collider but that is impossible I think. other than that my ideas that I didnt find how to implement are:
    find the collider that appears first,
    change the tilemap to have all tiles in bounds and simplify everything,
    or generate seperate polygonal colliders for each tile which is extremely inefficient I would assume but I dont know.