So what i am trying do is that i am trying to shoot a raycast that destroys another object. the problem is that the raycast is messed up and i dont know what to do now. pls help
public class CUT : MonoBehaviour
{
public TREE tree;
public Camera cam;
public GameObject PLAYER;
public bool EVENT;
public GameObject treeObject;
public LayerMask p_Layer;
void Update()
{
PLAYER = this.gameObject;
if (Input.GetMouseButtonDown(0))
{
Vector3 vector3 = cam.ScreenToWorldPoint(Input.mousePosition);
Vector3 mousePos = vector3;
Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
RaycastHit2D hit = Physics2D.Raycast(PLAYER.transform.position, mousePos2D, 50, p_Layer);
treeObject = hit.collider.transform.gameObject;
tree = treeObject.gameObject.GetComponent<TREE>();
EVENT = tree.CutEvent;
if (hit.collider != null)
{
Debug.Log("YUP");
EVENT = true;
}
if (hit.collider == null)
{
Debug.Log("NOPE");
}
}
}
}
this is the code and
public class TREE : MonoBehaviour
{
public bool iscuttable;
public string TreeType;
public int CutType;
public GameObject player;
public LayerMask P_layer;
[SerializeField] private float Circle;
public bool CutEvent = false;
GameObject ThisTree;
public void Update()
{
iscuttable = Physics2D.OverlapCircle(player.transform.position, Circle, P_layer);
if (CutEvent & iscuttable)
{
StartCoroutine(Enumerator());
}
}
IEnumerator Enumerator ()
{
yield return new WaitForSeconds(1);
ThisTree = this.gameObject;
Destroy(ThisTree);
}
}
this the code i am trying to access