SOLVED How do you interact with an object

Hello, I want to make the player walk up to an object and be able to trigger a small animation for it. For example: Walking up to a faucet and pressing E to make it turn on. Just a simple little decoration.

This is probably really easy to do but I'm kind of a beginner to GM2.
 
Last edited:

woods

Member
i would start with a distance_to_object check in your faucet.. if true, check for button press.. set image speed of faucet

something like this.. (untested but the logic flow is there)

obj_faucet create event
Code:
image_speed = 0;

obj_faucet step event
Code:
if (distance_to_object(obj_player) < 100) //check if player is close enough
{
if keyboard_check_pressed(ord(E)) // if so check for E being pressed
{
image_speed = 1; // change the speed of the faucet sprite so it animates
}
}
 
i would start with a distance_to_object check in your faucet.. if true, check for button press.. set image speed of faucet

something like this.. (untested but the logic flow is there)

obj_faucet create event
Code:
image_speed = 0;

obj_faucet step event
Code:
if (distance_to_object(obj_player) < 100) //check if player is close enough
{
if keyboard_check_pressed(ord(E)) // if so check for E being pressed
{
image_speed = 1; // change the speed of the faucet sprite so it animates
}
}
I've been messing around with your code all night and I can see it flicker to the desired sprite when I press E. However it just flickers back to the default sprite the second I press E. Any ideas?
 
Last edited:

WilloX

Member
GML:
if (distance_to_object(oPlayer) < 10) and keyboard_check_pressed(ord("E"))
    {
    if sprite_index == sFaucetOff
        {sprite_index = sFaucetOn}
    else
        {sprite_index = sFaucetOff}
    }
This should switch it as you probably want it to.
 
GML:
if (distance_to_object(oPlayer) < 10) and keyboard_check_pressed(ord("E"))
    {
    if sprite_index == sFaucetOff
        {sprite_index = sFaucetOn}
    else
        {sprite_index = sFaucetOff}
    }
This should switch it as you probably want it to.
Seconding his code

it actually checks to see if the facet is actually off or on when you hit the button so it should work

If it's off when you hit the button it will turn on and not move to the next function. If it's on it will ignore the first function and turn it off.
 
Top