Home > database >  How to stop raycast when there is a barrier blocking the object?
How to stop raycast when there is a barrier blocking the object?

Time:07-24

I'm using Raycast for picking up objects, whenever the player looking directly at a pickable object, the reticle colors turns to red meaning that it can be picked up. However, objects are still pickable even if there is a barrier between the raycast and the pickable object. like this image enter image description here

as you can see the drawer is closed and the pickable object is inside, but the player still can pick it up!. it should pe pickable only when the object is exposed and the raycast hits it directly like this:

enter image description here

    if (PickingUp) 
                 {
                if (heldObj == null) //if currently not holding anything
                {
                    
                    //perform raycast to check if player is looking at object within pickuprange
    
                    if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, pickUpRange) && hit.transform.GetComponent<canPickUp_>() != null)
                    {
                        
                        //make sure pickup tag is attached
                        // if (hit.transform.gameObject.tag == "canPickUp")
                       
                           
                             Debug.Log("Color should be changed"); 
                            if (hit.transform.CompareTag("TargetObj") && !targetObjectsList.Contains(hit.transform.gameObject))
                            {
                                /* if (aSource)
                                 {
                                     aSource.Play();
                                 }*/
                                targetObjectsList.Add(hit.transform.gameObject); 
                            }
                            PickUpObject(hit.transform.gameObject);
                       
                    }
                  
    
                }
                else
                {
                    
                    if (canDrop == true)
                    {
                        
                        StopClipping(); //prevents object from clipping through walls
                        DropObject();
                       
                        
                    }
                }

 private void PickUpObject(GameObject pickUpObj)
    {
        if (pickUpObj.GetComponent<Rigidbody>()) //make sure the object has a RigidBody
        {
            heldObj = pickUpObj; //assign heldObj to the object that was hit by the raycast (no longer == null)
            heldObjRb = pickUpObj.GetComponent<Rigidbody>(); //assign Rigidbody
            heldObjRb.isKinematic = true;
            heldObjRb.transform.parent = holdPos.transform; //parent object to holdposition
            heldObj.layer = LayerNumber; //change the object layer to the holdLayer
                                         //make sure object doesnt collide with player, it can cause weird bugs
            Physics.IgnoreCollision(heldObj.GetComponent<Collider>(), player.GetComponent<Collider>(), true);
            reticle.color = new Color(1, 1, 1, 0.75f);
            reticle.enabled = false;
            //transform.localScale = new Vector3(1 / heldObj.transform.localScale.x, 1 / heldObj.transform.localScale.y, 1 / heldObj.transform.localScale.z);
        }
    }

    void DropObject()
    {
        //re-enable collision with player
        Physics.IgnoreCollision(heldObj.GetComponent<Collider>(), player.GetComponent<Collider>(), false);
        heldObj.layer = 0; //object assigned back to default layer
        heldObjRb.isKinematic = false;
        heldObj.transform.parent = null; //unparent object
        heldObj = null; //undefine game object
        reticle.color = new Color(1, 1, 1, 0.75f);
        reticle.enabled = true;

    }

How would I prevent this from happening?

CodePudding user response:

As discussed in the comments the issue was that the "barrier" objects had their layers set to "Ignore Raycast", preventing the Physics.Raycast from reporting them as the hit.

Changing the layer to default (or any other layer that is not "Ignore Raycast") fixes the issue, as the "barrier" will be reported as the hit instead of the pickable object

  • Related