Look at Item and Press Interact to Pick up issues

Hi Guys, this is my first post hopefully its not too confusing.

I’m currently working through a Udemy course on unity “Ultimate guide to Game Development with Unity”. I have been adding a few features every now and again to make the game feel more natural/better. So I am up to a part where I wish to pick up a item (coin) by pressing the E key. I implimented it already but found if I add multiple coins in one area the game will pick them all up at once so I tried to make it so that if I look at the item I can pick it up. See the code below for reference:

private void OnTriggerStay(Collider other)
    {
        if (other.tag == "Player")
        {
            Ray rayOrigin2 = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
            RaycastHit hitInfo2;
            Physics.Raycast(rayOrigin2, out hitInfo2, Mathf.Infinity);
            if ((hitInfo2.transform == this.transform))
            {
                _uiManager.UpdateItemPickUpUI(this.gameObject);
                Debug.Log(hitInfo2.transform.name);
                if (Input.GetButton("Interact"))
                {
                    Player _player = other.GetComponent<Player>();
                    if (_player != null)
                    {
                        _player.ItemPickUp(this.gameObject);
                        AudioSource.PlayClipAtPoint(_coinSound, Camera.main.transform.position, 1f);
                        Destroy(this.gameObject);
                        _uiManager.RemoveItemText();
                    }
                }       
            }
            else
            {
                _uiManager.RemoveItemText();
            }
           
        }
    }

So basically I can only pick it up by looking at it. But when I enter game the text prompt (as seen below) never pops up unless I look upwards. Like either my coin is rising or my player is falling. But they are both stationary and on the ground. Any ideas what could be happening? Let me know if you want more details.
6672562--764062--upload_2020-12-31_2-51-55.png
I have a character controller attached to the player and modified the player movement method a little bit cause I was falling off the side of the map still.

Well to pick up something on “button click”,

why not shoot a raycast infront of your player and check whats infront of you?

Then create a type of class for Interactables: “public class Interactable: Monobehaviour”,

and whenever your raycast hits any interactable, you basically make the player able to pick it up

That’s weird. Is your canvas set to world rendering?

Some random steps to try and debug:

  1. When you drop in, pause the game and look around the scene. Where is the canvas?

  2. Put another fresh canvas in, put “HELLO!” on it and make sure you can see that at all times.

  3. Check if maybe you’re looking “backwards” at the ground and that is why you get the message? Check transforms used to do your “look,” however that is done.


These are my canvas settings. Will try those tests just now/tomorrow. Thanks Kurt!

Yeah Terraya, I am shooting a raycast while the player is in the objects collider. And if the player looks at the object they should be able to pick it up. I might make an Interactable class later but I am just learning still so maybe when I finished the course.

I did a Debug.Drawline (Debug.DrawLine(rayOrigin2.origin, hitInfo2.point,Color.red):wink:
for the raycast and it seems that the ray is coming from below the floor and hitting the collider. Also then I remembered the raycast is checking if it hit anything. So if I am already in the collider it shouldn’t work then should it? Might be worth reworking into a interactable class and have interactable radius instead of working with colliders (

)