I have 3D models on top of a table but if I am up at the table I can not pick up the objects. If I take a few steps back it works perfectly. Pretty new to raycasts so sorry if this is a easy answer or if I did something bad in my code.
public class PickUp : MonoBehaviour
{
public int distance;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
Grab();
}
}
void Grab()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
Debug.DrawRay(ray.origin, ray.direction * distance, Color.red, 100, false);
if (Physics.Raycast(ray, out hit, distance))
{
if (hit.transform.CompareTag("Item"))
{
Debug.Log("Item Hit");
GameObject ItemGrabbed = hit.transform.gameObject;
Destroy(hit.transform.gameObject);
}
}
}
}