How to only interact with an object when I'm looking at it

J

Jane Landrum

Guest
Hey guys! The title is pretty straightforward, I just want to be able to only read signs and stuff when I'm standing in front of it and the sprite is facing towards it, instead of just colliding with it anywhere. Any tips?
 
D

DarthTenebris

Guest
Check the coordinates and direction.

Sign collision event with player
Code:
if (((x < other.x) && (other.facing = -1)) || ((x > other.x) && (other.facing = 1))) { // if ((sign is on the left side of the player AND the player is facing left) OR (sign is on the right side of the player AND the player is facing right))
     // draw the text
}
Set the player's facing variable according to the appropriate movement code.

Hope I helped :)
 

woods

Member
@fat_man
that is way easier than my targetbox following infront of my player ;o)

i have a tile based game and was wanting to interact with things i was facing.. so i created a targetbox object that would 'follow in front of player"

simple sprite 32x32 same as my player object (made one red and one transparent so i could toggle the hitbox)

step event of target box
Code:
if instance_exists(obj_player)
   {
       depth = obj_player.depth-1
       dir = (obj_player.dir)
           
           if (dir = 0)
               {
               x = obj_player.x + 32
               y = obj_player.y
               image_speed=0; sprite_index=spr_target_box // right
               }
               else
           if (dir = 1)
               {
               x = obj_player.x
               y = obj_player.y + 32
               image_speed=0; sprite_index=spr_target_box // down
               }
               else
           if (dir = 2)
               {
               x = obj_player.x - 32
               y = obj_player.y
               image_speed=0; sprite_index=spr_target_box // left
               }
               else
           if (dir = 3)
               {
               x = obj_player.x
               y = obj_player.y - 32
               image_speed=0; sprite_index=spr_target_box // up
               }
   }
and then had simple collision check with targetbox for things i want to read/pickup/examine etc...
 
J

Jane Landrum

Guest
Check the coordinates and direction.

Sign collision event with player
Code:
if (((x < other.x) && (other.facing = -1)) || ((x > other.x) && (other.facing = 1))) { // if ((sign is on the left side of the player AND the player is facing left) OR (sign is on the right side of the player AND the player is facing right))
     // draw the text
}
Set the player's facing variable according to the appropriate movement code.

Hope I helped :)
I need a little help integrating it into my code if you don't mind. First of all I probably should've mentioned that my game isn't a side scroller (like Terraria or Shovel Knight or any of those games), it's more like Undertale or Stardew Valley where you can walk up and down and there's depth to it. I want to be able to stand in front of the sign to read it instead of to the left or right. Sorry about that I should have clarified in the first place!

The way I have everything set up is with a parent (oParentTalk) that is the parent of all the signs or anything I will need to interact with (NPCs, etc.). I already have it drawing all the text in the Step event, so should I copy and paste all of the step event into the collision event? Or should I put this code into the Step event? Or is there a way to say "go to the step event" that I could put in the collision event within the if statement? Sorry for the confusion.
 

woods

Member
I want to be able to stand in front of the sign to read it instead of to the left or right. Sorry about that I should have clarified in the first place!



how are you handling your player facing direction?
 
D

DarthTenebris

Guest
I need a little help integrating it into my code if you don't mind. First of all I probably should've mentioned that my game isn't a side scroller (like Terraria or Shovel Knight or any of those games), it's more like Undertale or Stardew Valley where you can walk up and down and there's depth to it. I want to be able to stand in front of the sign to read it instead of to the left or right. Sorry about that I should have clarified in the first place!

The way I have everything set up is with a parent (oParentTalk) that is the parent of all the signs or anything I will need to interact with (NPCs, etc.). I already have it drawing all the text in the Step event, so should I copy and paste all of the step event into the collision event? Or should I put this code into the Step event? Or is there a way to say "go to the step event" that I could put in the collision event within the if statement? Sorry for the confusion.
Not quite sure as I never tested this, but I think adding a check on the y axis should deal with up and down movements.
Code:
if (((x < other.x) && (other.facing == "-1")) || ((x > other.x) && (other.facing == "1")) // handle x axis, -1 is left and 1 is right
   || ((y < other.y) && (other.facing == "-i")) || ((y > other.y) && (other.facing == "i"))) { // handle y axis, -i is up and i is down
    // draw the text
}
Hope I helped :)
 
J

Jane Landrum

Guest
Not quite sure as I never tested this, but I think adding a check on the y axis should deal with up and down movements.
Code:
if (((x < other.x) && (other.facing == "-1")) || ((x > other.x) && (other.facing == "1")) // handle x axis, -1 is left and 1 is right
   || ((y < other.y) && (other.facing == "-i")) || ((y > other.y) && (other.facing == "i"))) { // handle y axis, -i is up and i is down
    // draw the text
}
Hope I helped :)
So how I do player facing is I have 4 different sprites for walking up, down, left, and right. Then in the player step event I have it to where the image_speed is movement speed/3..so if the movement speed is 0 then the image speed is 0 and it just stops on frame 0 which is the one that just faces that direction.

I was a little confused about what to do with the variable 'facing' that's used in the code above. I tried changing it to oPlayer.sprite_index == sPlayerR and changed it for all the directions but that didn't work. Also I'm still having the problem of I can't use a collision event because I'm already drawing the text in a step event. I'm sorry if I'm just not getting it and it's a simple solution but I appreciate all y'all's help!
 
D

DarthTenebris

Guest
Player Create Event:
Code:
facing = "1"; // because the default angle and thus direction is right
Player Step Event:
Code:
if (keyboard_check(ord("W")) {
     // or whatever your movement code is
    facing ="-i";
}
... do the same for all the other directions

if (facing == "1") {
   sprite_index = player_facing_right_sprite;
} else if ... do the same for all the other directions
I was a little confused about what to do with the variable 'facing' that's used in the code above.
The variable facing belongs to the player, not the sign, in case you got it mistaken.

Drawing the text should be handed over to the sign, otherwise the code I provided won't work. Also it makes more sense for the sign to do the drawing anyway (to me at least).
Sign Draw Event:
Code:
// the code in previous reply
because I'm already drawing the text in a step event
All drawing should be handled in the draw event, no exceptions at all. Otherwise it won't be drawn at all.

Hope I helped :)
 
Top