Raycast not work properly on y axis

I want to work my hand icon when im looking directly on apple, it should appear when my raycast hit on my Apple which is tagged as Apple, and currently its work well on horizantal axis but not work properly on vertical axis

my Hand icon continue appear when my cursor on vertical axis which is above the apple I’m not even looking at directly on Apple, my apple collider and script lookin ok where am i doing wrong I am using Unity’s new Starter Assets - First Person Character Controller ( as cam, player etc.) from Asset Store and I’m add my script on it.

ezgif.com-gif-maker (2)

collider

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

public class Pickups : MonoBehaviour
{
    RaycastHit RChit;
    [SerializeField] float Distance = 4f;
    [SerializeField] GameObject PickupTextAndIcon;

    private float RayDistance;
    private bool isPlayerRayHit=false;
    void Start()
    {
        PickupTextAndIcon.gameObject.SetActive(false);
        RayDistance=Distance;
    }

    
    void Update()
    {
        PickupApple();
    }

    void PickupApple()
    {
        if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out RChit, RayDistance))
        {
          if(RChit.transform.tag=="Apple")
          {
            isPlayerRayHit=true;
          }

          else
          {
            isPlayerRayHit=false;
          }
        }

        if(isPlayerRayHit==true)
        {
            PickupTextAndIcon.gameObject.SetActive(true);
            RayDistance=1000f;
        }

        if(isPlayerRayHit==false)
        {
            PickupTextAndIcon.gameObject.SetActive(false);
            RayDistance=Distance;
            
        }
    }
}

Problem solved I accidentaly put my script on my FPScontroller instead of camera lol :sweat_smile:

1 Like

Privacy & Terms