Problem is outdated
Space Battle AI issues
Hi all, I've been working on a space battle simulation, and managed to get quite far into the development, but 'm struggling a bit and hoped somebody smarter than me may be able to help with some a.i. queries?
I've uploaded a video of a space battle here: https://youtu.be/Ga1bfSu7mgA
I have various states Seek, Attack, Evade and Orbit. I'm very happy with progress, but there are a couple of issues ive tried to resolve but struggling:
Sometimes a ship flies parallel with its target instead of aiming towards it and firing.
Switching from Seek to Orbit (moon) doesn't Lerp smoothly.
Ships sometimes get stuck flicking between two states, causing a glitch
Battles move too far away from the moon.
Sometimes the ships seem to ignore targets
The state machine part of my code is below:
//If target is further than maxDist we lost the target if (target != null && target != moon) if ((target.transform.position - transform.position).magnitude >= loseDist) { target.GetComponent().targeted -= 1; target = null; TargetName = null; }
if (target == null || target == moon || mode=="Seek")
{
Transform temptarget = null;
// Look for a nearby target - or target nearer than curent
if (target != null && mode == "Seek")
{
temptarget = target;
}
nearest = loseDist;
for (int i = 0; i < enemyShips.Length; i++)
{
EnemyShip curShip = enemyShips[i].GetComponent<EnemyShip>();
if (enemyShips[i].gameObject.name != null && (enemyShips.Length < playerShips ? curShip.targeted < 5 : curShip.targeted < 2))
{
distance = (enemyShips[i].transform.position - transform.position).magnitude;
if (distance <= nearest)
{
nearest = distance;
target = enemyShips[i].transform;
enemyShips[i].GetComponent<EnemyShip>().targeted += 1;
TargetName = target.name;
}
}
}
// If no nearby enemies, return to moon
if (target == null)
{
if (temptarget != null) target = temptarget; else target = moon;
TargetName = "Moon";
}
}
distance = (target.transform.position - transform.position).magnitude;
if (target != null && target != moon)
{
if (distance <= minDist && mode != "Evade")
{
mode = "Evade";
randTarget = (int)Random.Range(0, 19);
}
else if (mode == "Seek" && distance <= fireDist && mode != "Attack")
{
mode = "Attack";
}
else if (mode == "Evade" && distance >= fireDist) mode = "Seek";
else if (distance <= maxDist && distance > fireDist && mode != "Seek")
{
mode = "Seek";
}
}
if (target == moon)
{
if (distance > 80) mode = "Seek"; else mode = "Orbit";
}
if (distance >= fireDist)
foreach (LaserScript ls in emitters) {
ls.firing = false;
}
if (target == moon)
{
foreach (LaserScript ls in emitters)
{
ls.firing = false;
}
}
The actions for each state below:
switch (mode) { case "Seek": { // Stop firing cannons foreach (LaserScript ls in emitters) ls.firing = false;
// If too far from moon, turn towards it, otherwise turn towards target
moondistance = (moon.transform.position - transform.position).magnitude;
if (moondistance > loseDist) SeekTarget(transform, moon);
else SeekTarget(transform, target);
//Fly forwards
MoveToTarget();
break;
}
case "Orbit":
{
// Stop firing cannons
foreach (LaserScript ls in emitters) ls.firing = false;
Orbit();
break;
}
case "Evade":
{
// Stop firing cannons
foreach (LaserScript ls in emitters) ls.firing = false;
// choose a random target near the moon and peel off in that direction
EvadeTarget(transform, Moon.targets[randTarget].transform);
// Fly forwards
MoveToTarget();
break;
}
case "Attack":
{
// Turn towards target
SeekTarget(transform, target);
//Fly forwards
MoveToTarget();
// If within firing range, start firing
if (distance <= fireDist) FireAtTarget();
break;
}
}
Thanks in advance
Follow this Question
Related Questions
Semi-realistic space flight model using multiple thrusters 2 Answers
Multiplayer Variable System 0 Answers
Please help to smooth player movement. 0 Answers