Gw Temp

Menu

Tutorial - 'Field View' by Guest

An item about Gamemaker posted on

Blurb

A tutorial about making a camera and setting off an alarm when it sees the player in Game Maker. Kudos!

Body

Hello makers!
Here's an explanation on how to make an object having a field of view, to e.g make a camscope which plays an alarm if it sees the player.(for topview games)
First create your main character. I'll call it "player".
Now create your camscope object. Let's assume it will rotate 360 degrees counter clockwise to watch an area :
In create event:

facing_dir = 0;

In step event (or alarm[0] event to avoid slowing the game) enter this piece of code :

facing_dir = (facing_dir + 10) mod 360;//10 is the rotation speed
image_single = facing_dir*image_number/360;//when using an animeted sprite with its first image facing right
p_dir = point_direction(x,y,player.x,player.y);
scale = 5;//with a small amount the game will slow down, with a big amount your camscope

if (distance_to_object(player) < 200)
if (p_dir > (facing_dir - 30 + 360) mod 360
&& p_dir < (facing_dir + 30) mod 360)
for (i = scale; i < 200; i += scale){
obj = instance_position(x + lengthdir_x(i, p_dir), y + lengthdir_y(i, p_dir));
if (obj.solid)
break;
if (obj.object_index == player){
global.alert = 1;
if (!sound_isplaying(snd_alarm))
sound_loop(snd_alarm);
alarm[0] = 5;//if you've set this script in this event
alarm[1] = 500;//to stop the alert if noone can see the player
}

In alarm[1] event:

global.alert = 0;
sound_stop(snd_alarm);

That's it!
Now when the player enters the camscope FOV the alert will be set, then stopped after a short period.
Later I will try to make the ennemies running towards the player when the alarm sounds.
See you!