How do I make a enemy have AI?
I was thinking of haveing a set path like from A to B but I also want them to shot?
I saw the Dogfight 3D game so I know its possible.
Offline
You can use the "distance to" sensor to tell how far away a sprite is from an enemy sprite, to trigger an attack. And the "direction" sensor to point the attack in the right direction.
Offline
Ok but how do I get it to move like lets say I want a gaurd paceing up and down the hall if he sees you he stops and shots you. Also how do I make walls?
Last edited by Slacker101 (2007-07-24 12:20:58)
Offline
Step 1 - lets get the patrol route sorted out.
There are a number of different ways of doing this, depending on how sophisticated you want to get.
You want your guard to be able to stop walking part way through his patrol, so you want his movement to be carried out in a number of small steps. These steps will be repeated till he reaches the end of his patrol, then he turns around and it starts again.
How to detect when he needs to turn around? It could be by touching a sprite or colour (perhaps representing the wall) or it could be by location.
**********
By Sprite:
Patrolling between Waypoint1 and Waypoint2. Your guard will walk in one direction till he touches Wall1, then turn around and walk towards Wall2
When green flag clicked
Set direction to (direction to Waypoint2)
Forever
- If touching Waypoint1
- - Set direction to (direction to Waypoint2)
- If touching Waypoint2
- - Set direction to (direction to Waypoint1)
move 10 steps
*******
Alternatively, you might want him to go left till he hits a wall, then turn around and walk right until he hits a wall. Assumign the walls are drawn in black on the stage:
When green flag clicked
Set direction to -90
Forever
- If touching colour black
- - change direction by 180
- - move 10 steps <-- this isto get your sprite clear of so it doesn't turn again)
- move 10 steps
*******
Finally, walking between 2 points: Say you want him to patrol between X:0, Y:0 and X:100, Y:0.
When green flag clicked
Set direction to -90
Forever
- If X<0
- - Set direction to 90
- If X>100
- - Set direction to -90
move 10 steps
*****
The last is good if you want a route that has no visible boundies, and the guard isn't walking all the way up to walls before turning.
The second is nice and easy if you are using definate walls.
The first is particularly good if your path is not exactly horizontal or vertical - the two destination sprites can be anywhere on the screen, and if they are the same colour as the background the player won't be able to see them. Its easy to add more waypoints, giving your guard a complicated route.
EG: A triangular route:
Forever
- If touching Waypoint1
- - Set direction to (direction to Waypoint2)
- If touching Waypoint2
- - Set direction to (direction to Waypoint3)
- If touching Waypoint3
- - Set direction to (direction to Waypoint1)
move 10 steps
******
Try those out and see which works best for you, then we can look at shooting, and walls.
Offline
This maybe really dumb to ask but how do I make a wall? I've made walls that teleport you somewhere else but not solid regular walls. Also I need to stop and shoot is there any way to do that?
Last edited by Slacker101 (2007-07-24 14:54:48)
Offline
Making a wall is as simple as drawing it on the background, or creating it as a sprite.
The tricky part is then telling your sprite to not walk though it. Basically, whatever you are using to tell the sprite to move forwards needs to be ammended to say "but don't move if I'm touching the wall".
Look at the pacman game that comes with scratch for one example of how to do it.
That uses a small coloured dot, and constantly checks to see if that dot is in empty space. If it is, it lets the pacman move forward, but if it isn't, it doesn't let it move forward.
Say your sprite has a red dot in front of it, and your clear paths are white, your script might look like this.
Forever
- if <<Key Up arrow pressed> AND <colour red over colour white>>
- - Move forwards 10
Offline
For stopping and shooting - get your movement and walls sorted out first then we can look at the rest.
Offline
Ok all done the sentry is moveing and the good guy isnt going through walls. Many thanks. Also made a title screen with difficulty settings.
Last edited by Slacker101 (2007-07-24 22:14:21)
Offline
I still need to know how to get baddies to stop and shot at you. Plus I would like to know were people get the old Atari and NES sounds from.
Last edited by Slacker101 (2007-07-25 16:30:55)
Offline
OK, you need to choose a detection method. How is your guard going to see you?
You can do it with distance, or a combination of distance and direction, usign the "distance to " and "direction to" sensor blocks.
You could do it by comparing sprite X and Y values (probably more complicated)
Or you could do it by having a "searchlight", a cone-shaped sprite emanating from the guard like a torch-beam, that triggers teh shooting if its touching you.
I think the latter has a certain elegance to it, especially since the "torchbeam" doesn't have to be visible to the player.
Offline
I have actually thought about that searchlight concept but I'm haveing trouble and I have also thoguht of a way of haveing something fire someting else but it must be standing still(the shooter) but it seems the sprite I have assigned to be the projectile isn't recieving the broadcast that is supposed to be being sent.I do have an idea on how to fix that though. And it worked.
Last edited by Slacker101 (2007-07-25 17:14:23)
Offline
Can't figure out how to make a moveing sprite stop to fire abd then fire.
Offline
How are you going to decide its time for the guard to shoot? Is he shooting at random interavals, or have you chosen a method for him to detect the player-sprite?
Offline
I was thinking of if distance to sprite is under 200 then show (want to make it wait) and shoot.
Offline
OK - inside your movement loop you need a check to see if the player is close enough.
So if using the "waypoint method" I posted above:
Forever
- If touching Waypoint1
- - Set direction to (direction to Waypoint2)
- If touching Waypoint2
- - Set direction to (direction to Waypoint3)
- If touching Waypoint3
- - Set direction to (direction to Waypoint1)
- move 10 steps
[b[- If <(distance to playersprite)<200)
- - Broadcast "Guard1shoots" and wait[/b]
Note that needs to be inside the "forever" loop, immediately after the move 10 steps, *not* after the end of the forever block.
That will make the guard stop until the actions attached to a "When I recieve [guard1shoots]" block are completed.
Then you need a shooting method.
This can be a costume change on the guard sprite with a shooting "beam" - laser or stream of bullets on it (crude but effective)
Or a new sprite whose scripts cause it to launch at the player sprite and travel in that direction until they hit it or an obstacle or the edge of the screen.
Offline
A shooting script can be found here, btw:
http://scratch.mit.edu/projects/Mayhem/7672
Offline
Example patroling/shooting guard here:
http://scratch.mit.edu/projects/Mayhem/26727
Offline
Slacker101 wrote:
How do I make a enemy have AI?
I was thinking of haveing a set path like from A to B but I also want them to shot?
I saw the Dogfight 3D game so I know its possible.
ur interested in AI? visit my friends gallery, AI tournament. http://scratch.mit.edu/galleries/view/18028
Offline