Search Unity

On Screen: Press 'E' to pick up

Discussion in 'Scripting' started by mrdbuffalo, Jul 24, 2016.

  1. mrdbuffalo

    mrdbuffalo

    Joined:
    Mar 6, 2016
    Posts:
    7
    So I have a basic pick up and drop object script (JavaScript) and it's all good but I want it to say " Press 'E' to pickup " in the middle of the screen with a custom font when I look at this object. I don't know if this is a bit to exact but I just want a maybe a article or a video or even just a push in the right direction for what I would be using for this as I have just go in to Unity again and a bit rusty, thanks for the help
     
  2. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    Cast a ray using Physics.Raycast, from your camera forward. If it hits the collider of the object, set a bool to true, but set it to false at the start of Update() (before the Raycast). Enable the UI text element that is positioned near the center of the screen if the bool is set to true but it isn't enabled yet, and vice versa.

    So what you need to know is:
    Physics.Raycast
    UI

    The Scripting API and User Manual are your friends :)
     
    Lethn likes this.
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    or just use a trigger collider and OnTriggerStay(...) and not have a test run every frame when they're no where near the pickup.

    Are we there yet? are we where yet? are we there yet? ...
     
    MikeTeavee and xjjon like this.
  4. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    True. However, it's debatable if casting a single ray every frame is preferrable to having at least 1 additional collider in the scene; it's not like the pjhysics engine doesn't also has to compute (non-)colliions every frame "behind the scenes".
     
    LeftyRighty likes this.
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you're assuming a single known pickup. If you've got a variable number (they spawn during runtime, they're picked up and destroyed/disabled) you'll need to be doing entire scene finds (at worst) or implement some sort of registry pattern to known of all the pickups that exist in the current frame to know they exist before raycasting to each of them; or flip the direction and have the pickups raycast to the player. All of which just feels "messy" to me since the driving force behind the interaction is the player colliding with them.
     
  6. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    I think you misunderstood part of the OP. He just wants to make some text appear when he looks at an object. That, to me, means that the object has to be in front of him where his crosshair or whatever is pointing at, hence the raycast solution. It's not neccessary to use colliders or cast rays to all item objects. At least that's how I understoof it :)
     
  7. mrdbuffalo

    mrdbuffalo

    Joined:
    Mar 6, 2016
    Posts:
    7
    Yeah it's like if an item is like 1 meter way I want to have a bit of text telling players what to do. Thanks for all the help both of you
     
  8. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    I use a method similar to what LeftyRighty suggested; turn the prompt on when the player enters the trigger area (OnTriggerEnter()) and turn it off when the player leaves it (OnTriggerExit())
     
  9. jasmith08

    jasmith08

    Joined:
    Oct 23, 2018
    Posts:
    1
    Hi I was really Hoping and Wondering is it possible if you can maybe share this pick up object with 'E' as I can find it anywhere... Thanks