Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Apr 30, 2019 at 11:55 PM by KevRev for the following reason:

Problem is outdated

avatar image
0
Question by KevRev · Apr 12, 2019 at 12:18 PM · spaceshipbattle-systema.i

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:

  1. Sometimes a ship flies parallel with its target instead of aiming towards it and firing.

  2. Switching from Seek to Orbit (moon) doesn't Lerp smoothly.

  3. Ships sometimes get stuck flicking between two states, causing a glitch

  4. Battles move too far away from the moon.

  5. 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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

167 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How do I slow down a quickly moving spaceship 0 Answers

game objects leaving trail of it's self 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges