Tiles are only being removed if raycast fired between 0 and 90 degrees clockwise from character
I'm making a 2d side-view game an I want a mining mechanic so that when the user clicks with the mouse it progressively mines towards the mouse until it hits the reaching distance. To do this I have a tools script on my player which checks for clicks and fires a ray in the mouse's direction for the reach distance, with a specific layer mask. It gets the hit point and feeds that to a method in another script on the tilemap. This script converts the position to a cell position and sets that cell to a given tile (in this case 'null').
The problem is the mining works if I click 0-90 degrees clockwise of my character and very, very occasionally 270-0 degrees clockwise of the character, but if I click in any other direction it doesn't work. I don't know what the issue is please help. If you need clarification just ask.
Video showing my problem: Click Here
My code:
PlayerTools script (on the player):
using UnityEngine;
public class PlayerTools : MonoBehaviour
{
public float health = 25f;
public float reachDistance = 3.5f;
public string toolType = "Pick";
public TileManager tileManager;
public LayerMask groundMask;
private float offset = 0.25f;
public LineRenderer line;
private void Update()
{
//tools
if(toolType == "Pick" && Input.GetMouseButtonDown(0))
{
Debug.Log("Click!");
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 dir = (mousePos - transform.position).normalized;
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, dir, reachDistance, groundMask);
Debug.Log("Dir: " + dir);
if (hitInfo)
{
Vector2 point = hitInfo.point;
Debug.Log("Point: " + point);
Debug.Log("Name: " + hitInfo.collider.name);
tileManager.SetTileAtPosition(point, null);
line.SetPosition(0, transform.position);
line.SetPosition(1, point);
}
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, reachDistance);
}
}
TileManager script (on tilemap):
using UnityEngine;
using UnityEngine.Tilemaps;
public class TileManager : MonoBehaviour
{
public Tilemap tilemap;
public void SetTileAtPosition(Vector3 position, Tile tileType)
{
Vector3 z0position = new Vector3(position.x, position.y, 0);
Vector3Int cell = tilemap.WorldToCell(z0position);
Debug.Log("cell: " + cell);
tilemap.SetTile(cell, tileType);
}
}