# Game Idea: How To Do?

I’m making an FPS game and I want to have objects flying in the air and attacking my character in some sort of random way. I’m quite new to the BGE so I don’t know the best way to do this.

Can it be done with a particle system? I want to be able to shoot the objects so I don’t know if this will allow it.

I don’t want the objects to be flying on the same paths every time, or the game will get boring very quickly. If I have to learn some python script to do this, I’ll try that, even though I’m new to python as well (but not to programming).

Any ideas are welcome.

Draw a cuboid, I mean an abstract one. You need two XYZ vectors, one for the origin and another for volume. Now dice roll three floats within range of these bounds, and you get spawn point coordinates. Dice roll again, and you get the coordinates for a goal to fly to. Track to goal, get there, re-reroll. Rinse and repeat.

``````from mathutils import Vector
from random import uniform

zvec3 = Vector([0,0,0]);

class SpawnBounds:
def __init__(self, origin, volume):
self.min = Vector([ origin.x - (volume.x * 0.5),
origin.y - (volume.y * 0.5),
origin.z - (volume.z * 0.5) ]);

self.max = Vector([ origin.x + (volume.x * 0.5),
origin.y + (volume.y * 0.5),
origin.z + (volume.z * 0.5) ]);

def randPointInside(self, min_off = zvec3, max_off = zvec3):
return Vector([ uniform(self.min.x + min_off.x, self.max.x + max_off.x),
uniform(self.min.y + min_off.y, self.max.x + max_off.y),
uniform(self.min.z + min_off.z, self.max.x + max_off.z), ]);

``````

Note how I added an optional offset parameter to `randPointInside()`. That’s one way to make sure the new coordinate is at least offset far enough from the last. You may come up with something less verbose yourself.

Final note, if the flying objects can collide with each other then you’re in for a good time. I don’t know if the built-in obstacle avoidance is going to be much help here and python isn’t the best fit for writing out your own.

I think navigation meshes might only move the enemies on surfaces and not volumes, but at least it’s a good start.

Thanks for this. It will get me started.

What’s a cuboid?

Eh, might not be the best term as definitions vary. I mean strictly a cubical shape whose points are not equidistant, that is to say the edges along each axis are not the same length. In other words, the tridimentional equivalent of what a rectangle is to a perfect square.

Like this.

Understood.

um i’d just generate them using random module, and calling them into the scene( then you can keep using the same obj over and over which) use a raycast to remove obj from that scene. and have a cube so if anything collides with cube it gets removed from scene e.g. deleted. simple really.

How do I make them fly on random courses?

you’ll have to be more specific, as i’m uncertain of the behaviour you’re looking for.

Well I just want some enemy objects to be flying on random courses through the air, attacking my player. Maybe not even random courses, but instead spawning at ransom locations but not flying directly (straight line) to the player. I was thinking of like the way boids act. I actually want to use boids if possible.

you can have them spawn in front of the player and move towards them.
something like this would work plenty:
import bge
import random
scene = bge.logic.getCurrentScene()

cont = bge.logic.getCurrentController()
own = cont.owner
obj.worldPosition.x = own.worldPosition.x+random.randint(-5,5)
obj.worldPosition.y = own.worldPosition.y+random.randint(-5,5)

then set your enemy object to “seek” the player using logic bricks.

if you’d like them to follow a specific course there’s many ways you can do that, one of which is to set a path they follow, another is to physically code in a pattern, for truly random code you’ll have to get further in depth.

1 Like

I appreciate the help. I’ll see what I can do with that code. I’m pretty new to using python but I think I can figure it out.

feel free to seek me out if you require further assistance.

1 Like