How to make game objects detect when you cross their vision? (SOLVED)

Hi guys im back

Im creating a level in which stationary/rotating/moving enemy game objects have a line of vision and if the player goes in that line of vision, the game is over. It’s a top down 2D game. How do I go about creating a script for that?

For rotating enemies, the line of sight will move with and the moving enemies, the line of sight will move with them as well.

Look at this example for field of view

You can give any object you want to have a field of view like this. In this example, it first checks if the objects are close enough, then if they are it checks if it’s in the field of view of the player object based on the player’s look direction and how wide its fov is.

To add line of sight you need to add a collision detection raycast with geometry step.

1 Like

EDIT: I got it to work! Only thing is that it rotates on the center of the object. Any possible way, from the code that I added, that I can get it to rotate on the left side of an object. The game object is a light sprite (a flashlight) and I’d like it to rotate from the one end of it instead of the center. Kind of like how your hand rotates on your wrist. Thanks

function init(self)
	go.animate(".", "euler.z", go.PLAYBACK_LOOP_FORWARD, 45, go.EASING_LINEAR, 4)
end

I think if you offset the sprite from the game object you’d get the visual look that you’re after.

1 Like

Thank you!

@britzl

Sorry for bringing this back up, I am having trouble with the angle part of it. It’s not rotating at the 45 degree angle I’d like it to. In my code above, 45 is the degree right?

Is it rotating at all? If yes, then how does it rotate?

Yes, 45 is the degree.

It rotates but the angle is thrown off. I have it at currently 1.75 instead of 45 and it’s hitting what would be 60 degrees. Unless I just didn’t position the sprite right but I think I did. The sprite rotates at its tip, like you said, so maybe it’s different from there?

Hmm, not sure I understand your setup. This is what I tried:

03

function init(self)
	go.animate(".", "euler.z", go.PLAYBACK_LOOP_PINGPONG, 45, go.EASING_LINEAR, 3)
end

rotate_euler

2 Likes