Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | SnapCracklins |
Hello. I am working on script for a 2d horror game heavily inspired by Clock Tower and all movement is right and left, single axis. I have a monster who chases the player. My goal with this:
- The monster should query the space to see if the player is there.
- If not, they change direction.
- There are only 2 kinematic bodies, so collide_with_bodies is what I need.
Seems simple enough but my implementation is not working. I know it’s how I am coding it because the debug message I scripted is not even printing, my movement is working and no errors popping up. Collision layers, masks and z-index are the same for all objects involved. Can’t find out why it’s not working.
func line_of_sight():
var vision = get_world_2d().direct_space_state
var field = get_viewport().size
var spotting_right = vision.intersect_ray(self.global_position, Vector2(field.x,0), [self])
var spotting_left = vision.intersect_ray(self.global_position, Vector2(0,0), [self])
if "collide_with_bodies" in spotting_right:
print("Spotted!")
walk_right()
if "collide_with_bodies" in spotting_left:
print("Spotted!")
walk_left()