Page 1 of 1

Make AI shoot when player is in line of sight

PostPosted: Sat Mar 29, 2014 2:45 pm
by tvz
Hello,
I am working on a platformer in which we use guns, frags, etc.
How can i make AI/computer shoot only when player comes in line of sight :?: . If there is any object breaking the line of sight of AI then it should not shoot/fire and this applies for both directions.
Please help

Re: Make AI shoot when player is in line of sight

PostPosted: Sat Mar 29, 2014 8:45 pm
by skydereign
Have you looked at this? http://game-editor.com/forum/viewtopic.php?f=6&t=6067&p=42535

You need some way of determining line of sight, and then simply use an if statement in the actor's draw (or similar).

Re: Make AI shoot when player is in line of sight

PostPosted: Sun Mar 30, 2014 5:22 am
by tvz
Thankyou
but I did not understand only one function in his code
Code: Select all
float xspeed;
float yspeed;

void gothisway(float ang,float speed)
{
xspeed=-cos(degtorad(ang))*speed;
yspeed=sin(degtorad(ang))*speed;
}

I don't know how to USE cos, sin, degtorad functions and what they do. :(
May you please tell me their use

Re: Make AI shoot when player is in line of sight

PostPosted: Mon Mar 31, 2014 3:03 am
by CrackedP0t
cos() is Cosine, sin() is Sine. Cosine and Sine are used in trigonometry, if you didn't know, and have to to with determining side lengths of right triangles from angles. degtorad() is converting degrees to radians. I can explain a little trigonometry if you'd like, but if you really want a good understanding, you need to take a class on it.

Re: Make AI shoot when player is in line of sight

PostPosted: Tue Apr 01, 2014 4:56 pm
by tvz
Thankyou for your reply
I know about these sin and cos, these are used in right triangles to find the lengths of sides etc, but I did not understand what that code is doing in above post.

Re: Make AI shoot when player is in line of sight

PostPosted: Tue Apr 01, 2014 5:47 pm
by lcl
By looking at the code, it seems to be designed to make it possible to move an actor to a desired direction without using angle & directional_velocity but just by manipulating it's x and y coordinates or by using xvelocity and yvelocity.
What the function does, is to get the correct modification values for x and y and then storing them to variables xspeed and yspeed.

Re: Make AI shoot when player is in line of sight

PostPosted: Wed Apr 02, 2014 4:04 pm
by tvz
Now i understand what this code does.
Thanks
sin^2(θ)+cos^2(θ)
for you all