Enemys and Raycast

So I am making a game for a few days now and I am wondering how to make an enemy hover above the ground, but also to come towards me but not to near, so it stops and shots. And if anyone can please could someone post a .Blend of how to use raycast to use in a gun (damage enemy’s and the same back to me).

Thanks in advance.:):):).

Come on does anyone know :(.


if player_seen:
    if player_in_range:
        shoot_at_player()
    else:
        walk_toward_player()

I have posted a similar thing many times this past month: AI’s are too unique for a script someone gives you off the internet to have a chance of working properly in your game.

What Goran has done is built a very simple Finite State Machine (FSM), which is perfect for a simple AI like this. Obviously he hasn’t given the whole script, which would define the variables ‘player_seen’ and ‘player_in_range’ as well as the functions ‘shoot_at_player’ and 'walk_toward_player, but these are easy to find.
player_seen can be done with a simple raycast:


hit = enemy.rayCastTo(player)
if hit != player:
    player_seen = False
else:
    player_seen = True

Player in range is another simple few lines:

distance = enemy.getDistanceTo(player)
if distance > 15:
    player_in_range = False
else:
    player_in_range = True

Shoot at player is a little more complex, so I shan’t go into the details here, but what I would do is use getAxisVect to find the direction the enemy is facing, use a raycast to find what the bullet hit, and then subtract from a property (something like health).

walk_towards_player is a simple function, thanks to the navmesh feature, and you can simply activate a steering/navigation logic brick.

Heres my take on it with a blend about as simple as I can make it:

hover is done with logic bricks and range/shooting done with a small python script.
i did use a ray rather than constantly casting a rayCast



How it works:
Hover is done using a ray cast down a short distance. If the ray is positive. It applies upward force, taking it slightly above the ray distance for a small bobbing.

The enemy will constantly turn in a circle unless the player gets within a certain Range:
If the player is within the range the enemy will track to it and drive towards the player.
Then if the player gets within a ‘Firing Range’ the enemy will stop pursuing and start only firing and tracking.
There are lots of other ways to do this and expand on this.

Controls:
WASD Player Movement - Blue Block
Move near the enemy for him to pursue/attack you.

Note: you must be using GLSL to see the shadow under the enemy to see that its hovering.

Attachments

trackandshoot.blend (437 KB)

Thanks all of you for replying:D. Can I use yours in a game superFlip. sdfgeoff i get what you mean but I think I can implement SuperFlips a little easier, But could you tell me how I would use yours, so do i put the code in the same script or not.
P.S: I am learning python so I’m not so good at understanding it. :o.
P.P.S: Thank you Goran for posting it and I get your code and how it works, but I might try and use it.:D.

You can use what I wrote for anything you wish, If youll look at the scripts and examples you will see what Goran, sdfgeoff and I wrote have alot of similarities. Tinkering with and replacing different parts of the code would be good to learn with , such as replacing my forward_ray with sdfgeoff rayCasts and stuff