How to detect a game object in front of my player inside a peticular range,How to get the object in front of the player withing a range

Hi,
I’ve create a script for getting object nearest of the player because RayCastHit when you are too near in the range that stop detecting it.

I would like to only get the object in front of the player inside the range not around the player

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;


public class ObjectDetector : MonoBehaviour
{

    [SerializeField]
    private string _message; 
    public string MessageGet { get => _message; set => _message = value; }



    //detection range
    float range = 5;

    private GameObject target;

    List<GameObject> getObject = new List<GameObject>();

    // Start is called before the first frame update
    void Start()
    {
        GetAllObject();
    }






    // Update is called once per frame
    void Update()
    {
        GetClosestObject(getObject,this.gameObject);
        Debug.Log(Vector3.Distance(transform.position, target.transform.position));

        if (Vector3.Distance(transform.position + transform.forward, target.transform.position) <= range)
        {
            if (target.tag == "GetObject")
            {
                var objbehavior = target.transform.GetComponent<ObjectBehavior>();

                MessageGet = "Press (E) to get " + target.transform.name + " x" + objbehavior.Nombre;
            }
            else { MessageGet = ""; }

        }
        else { MessageGet = ""; }
    }


    public void GetClosestObject(List<GameObject> Object, GameObject fromThis)
    {
        GameObject bestTarget = null;
        float closestDistanceSqr = Mathf.Infinity;
        Vector3 currentPosition = fromThis.transform.position;
        foreach (GameObject potentialTarget in Object)
        {
            Vector3 directionToTarget = potentialTarget.transform.position - currentPosition;
            float dSqrToTarget = directionToTarget.sqrMagnitude;
            if (dSqrToTarget < closestDistanceSqr)
            {
                closestDistanceSqr = dSqrToTarget;
                bestTarget = potentialTarget;
            }
        }
        target = bestTarget;
    }

    public void GetAllObject()
    {
        GameObject[] items= GameObject.FindGameObjectsWithTag("GetObject");
        foreach (var item in items)
        {
            getObject.Add(item);
        }
    }

}

@AyataneIruka, there are some different ways to achieve this.

If you use a trigger for example, with some radius setup to the area for players to be considered, you can track the OnTriggerEnter and then do a Vector3.Dot check by using your character’s position and the other object’s position. About the scalar (float) returned by the dot product, if your target is behind you, that value will be negative, if 0, it’s because you’re 90 degrees from the target. While anything positive will mean, your target is “in front of you”. That range will be from 1 to 10, so you should clamp your range to mayne dotProduct >= 7.5 or something like that to reduce that valid range.