- Home /
Create spaceship attack logic [and other logic eventually]
I am curious how to implement a spaceship attack logic. I have states set up to do checks for each "command" each ship is given, but am unsure how to code logic for attacks.
Some ships will have to "orbit their targets", such as giant battle cruisers that have guns and armor on the sides, while others attack head on and then turn away to avoid collisions. The one I want to concentrate on is about fighters.
I want the fighters to do an "attack run", they will circle the target (clockwise or counterclockwise) and then line up a run to go towards the target and shoot missiles and such, and then once they get close enough to veer off and repeat the action (not necessarily from the same angle of attack however).
I am currently using the "Arongranberg a* pathfinding project" for my game movement ai. I want to essentially create a kind of bezier curve that is centered around the target that the attacking ship follows to perform this movement. I also want to take into account of obstacles to left or right when deciding which way to turn when breaking away from the target after an attack run. How would I do this without too much overhead cost in cpu (as my game will eventually go to mobile).
My ideas are to generate two bezier curve paths (one for turning left out of attack run and one for turning right out of attack run) and then just switching to the appropriate path. I also want to accommodate for the ship moving (but i'm sure i can just use waypoint offsets like its parented to the target). My other problem is to handle going clockwise or counterclockwise.
How would any forum-goers choose to do this, and can you provide an example code showing how to generate this waypoint path and how to modify it and switch between paths (i'm assuming it would just be an array of vector3 arrays).
PS: I want to avoid using unity's physics system as i want to maximize the number of agents i have and be careful in consideration for eventually going mobile so i don't have to change a ton later).
PPS: [The style of attack i want is similar to aircraft fighters or spaceship fighters from Sins of a Solar Empire or Homeworld 2]
Answer by flaviusxvii · Feb 07, 2013 at 04:43 PM
I'm making a starfighter dogfighting game and I've put a lot of thought into the flight paths of fighters and starships. I settled on letting the ambition of the "pilot" determine how the craft is steered. I put these on a stack and the various ambitions have successive priorities.. So they start with "Patrol", which has them steer towards a waypoint, and when they get close enough to it, they'll steer towards the next waypoint in a course that's set for them. If they detect an enemy, they'll push "Attack" onto their steering stack, and pursue the enemy, steering to get a good angle of attack. If they get shot, they'll push an "Evade" onto their stack (it's high priority, but has a time limit of 10 seconds), then that'll pop and they'll go back to pursuing their target, or sometimes they'll push an "Attack" specific to their attacker before they push the "Evade".
Anyway, each of these steering actions has it's own code for where to steer. Like I said, "Attack" is all about steering for a good angle of attack. "Evade" tries to fly wildly. Apart from patrol, there are no set "waypoints" or predetermined paths. The pilot chooses points in space to fly towards and then makes new choices based on the ambitions at the time.
Don't know if that helps, but I think it makes more sense than the approach you described.
http://bytefluent.com/starfighter <-- if you wanna try it.
Hmm, i chose to use a state machine (as it handles all that i need) but i find it interesting that a stack was considered. I do like the idea.
I think i'll just have a previous command (my commands are integers and maybe an enumerated value later) and is easy to do a switch statement on this.
I already use the a* pathfinding extension, so i figured i would find a way to do this with a path, and array of vector3 objects.
How would I make one of my ships generate this path to follow? Having a generated path also allows for easy checking of collisions and of other deciding factors.
I will look at your fighter code and see if i cna learn something, but i am curious how to generate a bezier curve path to do my attack movement.
Could you demonstrate or provide a nice example script for how you did your fighter code? I am unsure of how to implement logic and steering for good attack angles in the excerpt:
" Like I said, "Attack" is all about steering for a good angle of attack. "Evade" tries to fly wildly. Apart from patrol, there are no set "waypoints" or predeter$$anonymous$$ed paths. The pilot chooses points in space to fly towards and then makes new choices based on the ambitions at the time. "
How would i make this logic to steer it to attack? Can you provide a little example script or provide one that you have used?
[If your worried i will copy and just use it as i'm lazy to write my own, i just want to find a good algorithm and implementation on how to do this as idk where to start. I also prefer writing my own scripts if i can to avoid using others code.]