Picking up rigidbody objects

Hey there again! SO here is the deal! I made a script that i can attach to a rigidbody/object, and than, when the player comes to it, is looking at it, isnt hodling any other object and there is nothing in the way can pick up. It works perfectly. BUT I need to make something else. I need to make a code, thats on the player and checks if there is a rigidbody around, and if there is nothing between etc. So the same thing, just casted from the player. Reason why I wanna do it is, because I am checking all that things after player E key is pressed, but I need to make it check it realtime, because i wanna make a GUI text like Press E for pickup, so when there are 50 objects plus some other things, FPS and Draw calls will go crazy.

Here is the original code:

[1]


 [1]: https://docs.google.com/document/d/1RHTx7JSl7XN_LMk0xwScT35YV6PoHDsAYOW6vEIpQ34/edit?hl=en_US

Normally I wouldn’t write code from scratch for a question, but you got lucky because I’m feeling nice today.

Do something like this on a script somewhere on your player


`var fromWhere : transform;

var dist: float = 2;

var pickUpLayerMask : LayerMask;

function LateUpdate()

{

    var hit : RaycastHit;
    
    var fwd: Vector3 = fromWhere.TransformDirection(Vector3.forward);
    
    if(  Physics.Raycast(fromWhere.position, fwd, hit, dist, pickUpLayerMask)  )
    
       if(hit.rigidbody)
    
           if(hit.rigidbody.tag == "Insert tag that says I can be picked up here")
    
           {
    
              /*we have found a rigidbody that can be picked up in the center
              of the screen that is close enough to work with. do stuff */
    
           }
    

}
`

I normally only help with small things like bug fixes and answering questions, not building features from scratch like I did here, so keep that in mind for any further questions you ask here.

Also, I couldn’t access the code you put in the link in the question. Please try a different code pasting method next time.

All Credits to @SilverTabby. Thanks man!
I needed it in C#, so I translated it and now want to share it.

using UnityEngine;
using System.Collections;

public class PickUpScript : MonoBehaviour
{

    Transform fromWhere;


    float dist = 2;


    LayerMask pickUpLayerMask;

    void LateUpdate()
    {


        RaycastHit hit;

        Vector3 fwd = fromWhere.TransformDirection(Vector3.forward);

        if (Physics.Raycast(fromWhere.position, fwd, out hit, dist, pickUpLayerMask))
        { 
            if (hit.rigidbody)
            { 
                if (hit.rigidbody.tag == "Insert tag that says I can be picked up here")

                {

                    /*we have found a rigidbody that can be picked up in the center
                    of the screen that is close enough to work with. do stuff */

                }
            }
        }
    }
}