How to have player sprite activate switches/press in-game buttons.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GiantIsopod
:warning: Old Version Published before Godot 3 was released.

I’m relatively new to coding for video games, so I apologize ahead of time if the answer is obvious or the question is poorly worded. I’m trying to create a top-down 2D horror/puzzle game which involves a lot of the player character flipping switches or inspecting objects. However, I can’t figure out how to make it so that the player character can activate certain nodes when the player sprite is in close proximity and looking in the direction of the object’s on-screen sprite while the player presses a specified button. An example would be like how in Lara Croft the player would have to maneuver Lara in front of a lever, face the lever, and then press a certain button on the controller for Lara to activate the lever. Thanks in advance.

:bust_in_silhouette: Reply From: quijipixel

You need to use a Area2D to detect when the player is close to an interactive object. Add this node to the lever´s scene and add the body_enter signal. This signal gives you as parameter the object who entered the area (remember that the collision layers and mask must match to make the area and the players collision detectable. Check this post for more info on how that works). Then, once on the fired signal function, check if the object is facing towards the lever position. To achieve that you can use Vector math. Check this tutorial to see how to use the dot product to do that.

If the conditions are met, just activate a boolean variable. Poll the _input and check when the player pushes the action button. If that variable is set then there you are certain the player pulled the lever. Remember to make the body_exit signal to. If the player leaves the levers area, then turn the activate boolean variable off. (You don´t want the player magically pulling the lever from a far distance)

Thank you! That’s very helpful.

GiantIsopod | 2017-08-20 23:11