raycast picks up through wall (simple script included)

RaycastHit hit;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast (transform.position, fwd, out hit, rayLength, hitMask)) {

			if (hit.collider.CompareTag ("Consumable")) {
				CrosshairActive ();
				raycastedObj = hit.collider.gameObject;
				ItemProperties properties = raycastedObj.GetComponent<ItemProperties> (); 
				itemNameText.text = properties.itemName;

				if (Input.GetMouseButtonDown (1)) {
					properties.Interaction (playerVitals);
					Destroy (raycastedObj);
  • Check that the LayerMask for the raycast is correct.
  • Make sure that the raycast origin (the transform.position) is not inside the wall’s collider.
  • Debug the raycast so you can see it by using Debug.DrawRay().

Tip: use ‘transform.forward’ instead of creating the ‘fwd’ variable.

I created new tag “wall”,seems to work for now,thanks guys, :slight_smile:

 if (hit.collider.CompareTag ("Wall")) {
    itemNameText.text = null;