Announcement

Collapse
No announcement yet.

Pressing e to interact.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Pressing e to interact.

    Hi, so I have a simple box that adds you points to the hunger value. When i walk over it, it gets consumed so it's being destroyed. The thing is, I want to interact with it by the "E" key instead of just
    walking through the box. How can I do that?


    #2
    Instead of performing the hunger modification on overlap, instead you could get a reference to your third person character and set a variable reference to the box you walked into. Then add a new custom event in the box blueprint, something like "Activate". Move your hunger modification and box destroy code to this new event.

    When in your Third Person Character blueprint, add an event for the keyboard "E" key. If the box variable reference is valid, call the "Activate" event on the box.


    Some additional things will need to be done to ensure there's no problems. For example, with the above setup, a player could walk up to a box then walk away and press "E" anywhere to "Activate" it. You'll need to create another event in the box to unset the box reference on the character on On Component End Overlap.

    There is also the problem of when there are multiple overlapping boxes. If you enter an area where the player is colliding with two boxes, the player will only have a reference to one of them. And if you leave the area of the one you have a reference to so you're only inside the OTHER one, pressing "E" will have no effect.

    Probably the best way to deal with this in this setup is to use an array to contain the references to the boxes on the character. This array will mean "These are the boxes the player is close enough to interact with". Entering the box area will add a reference to the array, leaving the box area will remove a reference to the array. Pressing "E" could either use the box closest to the player (You'd have to loop through all boxes and compare the locations to the player to determine which is closest. Or I think you could use a dot product. It's not that hard) OR you could just use all boxes the player is near.

    ---

    An alternative setup is to re-think how the boxes are detected in the blueprints. You could change this so you don't do collision overlaps at all. Instead, when the player presses "E", perform a line trace from the player in the direction of the camera to see if there's a box ahead of the player. If there is, activate it.

    Anyway, sorry for rambling on a bit. I'm happy to help with any of these implementations. You could get really complex with this or keep it very simple and I don't want to assume what you want to do.

    Comment

    Working...
    X