Search Unity

Controller [Released] Sci-Fi Ship Controller - A versatile physics-based controller with playable demos, AI...

Discussion in 'Tools In Progress' started by sstrong, Dec 7, 2018.

  1. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    For anyone who won`t wait for LOS update:
    Ship.cs 2831 - replace Autofire IF with

    Code (CSharp):
    1.                             if (weaponFiringButtonInt == weaponAutoFiringInt && weaponTypeInt == 10)
    2.                             {
    3.                                 // Is turret locked on target?
    4.                                 isReadyToFire = weapon.isLockedOnTarget;
    5.  
    6.                                 var hit = GetLos();
    7.  
    8.                                 var FriendlyTags = "Player, Ally, Neutral";
    9.  
    10.                                 if(hit.collider != null
    11.                                     && !FriendlyTags.Contains(hit.collider.gameObject.tag))
    12.                                 {
    13.                                     isReadyToFire = true;
    14.                                 }
    15.                                 else
    16.                                 {
    17.                                     isReadyToFire = false;
    18.                                 }
    19.                             }
    And add somewhere below:
    Code (CSharp):
    1. private RaycastHit GetLos()
    2.         {
    3.             RaycastHit hit = default(RaycastHit);
    4.  
    5.             if (weapon.target)
    6.             {
    7.                 var weaponPosition = weaponWorldBasePosition +
    8.                     (trfmRight * weaponRelativeFirePosition.x) +
    9.                     (trfmUp * weaponRelativeFirePosition.y) +
    10.                     (trfmFwd * weaponRelativeFirePosition.z);
    11.  
    12.                 var targetDir = weapon.target.transform.position - weaponPosition;
    13.                 var maxDistance = weapon.projectilePrefab.startSpeed * weapon.projectilePrefab.despawnTime;
    14.                 Physics.Raycast(weaponPosition, targetDir, out hit, maxDistance);
    15.                 Debug.DrawRay(weaponPosition, targetDir, Color.red, 0.5f);
    16.             }
    17.  
    18.             return hit;
    19.         }
    NOT very efficient resource-wide but will work magic of no friendly-fire with autofire turrets.
     
    sstrong likes this.
  2. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    Thanks for posting. Yes, we are working on a more global performant solution. The gametag code will create garbage each frame (there is an alternate non-garbage solution for gametags).

    Where possible, use the Ship faction id to check for friend or foe. Integer comparisons are fast and don't generate garbage.
    Code (CSharp):
    1. shipControlModule.shipInstance.factionId
    You can set the factionId (and squadronId) in the inspector or at runtime.

    Also, you don't want to fire if weapon.isLockedOnTarget is false.
     
    Last edited: Feb 1, 2020
  3. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    FractionIDs were not in in release when i started this script)
    As i said - it`s not a release version, just a minor fix not to shoot yourself or your teammate)
    I`ll rewrite it further down the line for fraction IDs, and will keep you posted on turrets autotarget updates.
    Maybe you`ll implement\add some of those ideas into your asset later on :)
     
    sstrong likes this.
  4. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    Ok, thanks. FactionId have been there since early beta's but only exposed in the Editor since the last version. For a full list of what is available for a ship, take a look in ship.cs. All variables and public APIs are commented.
     
  5. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    We've seen some issues with Unity Input System 1.0.0 preview 4. If you see StackOverflowExceptions in the editor or the editor seems to hang for 20+ seconds, downgrading to Input System 1.0.0 preview 3 in PackageManager seems like a good option. We have logged it as a bug with Unity here.
     
    Silvermurk likes this.
  6. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    Looks like a bug - AI Ship Input script has Max Speed variable, but totally ignores it by reaching speeds far superior to max velocivy value.

    And Can you pretty please assist me in ajusting weapon script inside the Ship.cs to shoot at screen center ray at distance 100 instead ot fireing forward?

    UPD: Deadzone would also be very nice addition to UnityInput mouse pitch\yaw.
     
    Last edited: Feb 2, 2020
  7. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    Bad hotfix while i don`t know a better way to make weapons shoot to screen center, in case someone else needs it:
    Code (CSharp):
    1. RaycastHit hit;
    2.                                             //TODO - layermask and float distance
    3.                                             Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 1)), out hit, 1000f);
    4.                                             var fireVector = hit.point - RigidbodyPosition;
    5.  
    6.                                             if (hit.collider && weaponTypeInt != 10)
    7.                                             {
    8.                                                 // Create a new projectile using the Ship Controller Manager
    9.                                                 // Velocity is world velocity of ship plus relative velocity of weapon due to angular velocity
    10.                                                 shipControllerManager.InstantiateProjectile(weapon.projectilePrefabID, weaponWorldFirePosition,
    11.                                                     fireVector, trfmUp, worldVelocity + Vector3.Cross(worldAngularVelocity, weaponWorldFirePosition - trfmPos),
    12.                                                     gravitationalAcceleration, gravityDirection, shipId, squadronId);
    13.                                             }
    14.                                             else
    15.                                             {
    16.                                                 // Create a new projectile using the Ship Controller Manager
    17.                                                 // Velocity is world velocity of ship plus relative velocity of weapon due to angular velocity
    18.                                                 shipControllerManager.InstantiateProjectile(weapon.projectilePrefabID, weaponWorldFirePosition,
    19.                                                     weaponWorldFireDirection, trfmUp, worldVelocity + Vector3.Cross(worldAngularVelocity, weaponWorldFirePosition - trfmPos),
    20.                                                     gravitationalAcceleration, gravityDirection, shipId, squadronId);
    21.                                             }
     
  8. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    We've added a method which you can call to make it easier to adjust the Fire Direction of a weapon each frame (if required).
    Code (CSharp):
    1. shipControlModule.shipInstance.SetWeaponFireDirection(weaponIdx, wsTargetPosition);
    We'll upload it for beta program members tomorrow. We'll also add you to the program so you can download it.

    You can take your worldspace position you want to aim at pass it to the new method from your own code. It is always better not to modify our code or it will get overwritten every time we issue an update.

    You should be able to control deadzone settings through the Unity Input System (as I recall there is a setting panel provided by Unity).

    I'll let David help you with your velocity question.
     
    Silvermurk likes this.
  9. Dave_2000

    Dave_2000

    Joined:
    Oct 17, 2015
    Posts:
    52
    Yes, the max velocity problem looks like a bug. I found some errors in the code and fixed them, we'll upload a fixed version to the beta program tomorrow.
     
    Silvermurk likes this.
  10. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    I know, that`s why i`m posting it here:
    First reason is to keep it from being lost on version update and not to copy\paste it each time.
    Second reason - to let you know about problem\missing feature so it could be thought throu and either implemented or discarded as not really needed one)
     
  11. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    Updated target provider for autofire turrets. Now with all three types of target finding and trashin-trashout safety.
    Plus auto-aquesion of a new target upon destruction of preveous one or it`s position out of targeting range.

    Code (CSharp):
    1.  
    2. using SciFiShipController;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Linq;
    6. using UnityEngine;
    7. public class ShipAiTurretHelper : MonoBehaviour
    8. {
    9.     public float UpdateInterval = 0.5f;
    10.     public float TargetingRange = 500f;
    11.     public List<int> EnemyFactions = new List<int>();
    12.     public LayerMask EnemyLayer;
    13.     public string EnemyTag = string.Empty;
    14.     public GameObject Target = null;
    15.     private ShipControlModule targetShip;
    16.     private void Update()
    17.     {
    18.         if (Target == null)
    19.         {
    20.             Target = GetTarget();
    21.             if(Target)
    22.             {
    23.                 targetShip = Target.GetComponent<ShipControlModule>();
    24.                 if (targetShip)
    25.                 {
    26.                     targetShip.callbackOnDestroy += CallbackOnDestroy;
    27.                 }
    28.             }
    29.         }
    30.         if(Target != null
    31.             && Vector3.SqrMagnitude(Target.transform.position - transform.position) > TargetingRange * TargetingRange)
    32.         {
    33.             targetShip = Target.GetComponent<ShipControlModule>();
    34.             if (targetShip)
    35.             {
    36.                 targetShip.callbackOnDestroy -= CallbackOnDestroy;
    37.             }
    38.             Target = null;
    39.         }
    40.     }
    41.     public void CallbackOnDestroy(Ship ship)
    42.     {
    43.         targetShip.callbackOnDestroy -= CallbackOnDestroy;
    44.         Target = null;
    45.     }
    46.     private GameObject GetTarget()
    47.     {
    48.         Collider[] targets;
    49.         if (EnemyLayer != 0)
    50.         {
    51.             targets = Physics.OverlapSphere(transform.position, TargetingRange, EnemyLayer);
    52.             if (targets != null)
    53.             {
    54.                 return GetClosestEnemy(targets).gameObject;
    55.             }
    56.         }
    57.         else if (!string.IsNullOrEmpty(EnemyTag))
    58.         {
    59.             targets = Physics.OverlapSphere(transform.position, TargetingRange);
    60.             if (targets != null)
    61.             {
    62.                 var validTargets = targets.Where(x => x.tag == EnemyTag).ToArray();
    63.                 if (validTargets != null)
    64.                 {
    65.                     return GetClosestEnemy(validTargets).gameObject;
    66.                 }
    67.             }
    68.         }
    69.         else if (EnemyFactions != null && EnemyFactions.Count > 0)
    70.         {
    71.             targets = Physics.OverlapSphere(transform.position, TargetingRange);
    72.             if (targets != null)
    73.             {
    74.                 List<Collider> validTargets = new List<Collider>();
    75.                 foreach (var t in targets)
    76.                 {
    77.                     var ship = t.GetComponentInParent<ShipControlModule>();
    78.                     if (ship && EnemyFactions.Contains(ship.shipInstance.factionId))
    79.                     {
    80.                         validTargets.Add(t);
    81.                     }
    82.                 }
    83.                 if (validTargets.Count > 0)
    84.                 {
    85.                     return GetClosestEnemy(validTargets.ToArray()).gameObject;
    86.                 }
    87.             }
    88.         }
    89.         return null;
    90.     }
    91.     private Transform GetClosestEnemy(Collider[] enemies)
    92.     {
    93.         Transform bestTarget = null;
    94.         float closestDistanceSqr = Mathf.Infinity;
    95.         Vector3 currentPosition = transform.position;
    96.         foreach (var potentialTarget in enemies)
    97.         {
    98.             Vector3 directionToTarget = potentialTarget.transform.position - currentPosition;
    99.             float dSqrToTarget = directionToTarget.sqrMagnitude;
    100.             if (dSqrToTarget < closestDistanceSqr)
    101.             {
    102.                 closestDistanceSqr = dSqrToTarget;
    103.                 bestTarget = potentialTarget.transform;
    104.             }
    105.         }
    106.         return bestTarget;
    107.     }
    108. }
    109.  
    110.  
     
    Last edited: Feb 2, 2020
  12. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    And shield-recharge module, also perfomance-heavy at alpha.
    Code (CSharp):
    1. using SciFiShipController;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class ShipShieldHelper : MonoBehaviour
    7. {
    8.     public float ShieldDelay = 2f;
    9.     public float ShieldPerSecond = 1f;
    10.     private float LastDamage;
    11.     private ShipControlModule shipControlModule;
    12.  
    13.     private void Awake()
    14.     {
    15.         shipControlModule = GetComponent<ShipControlModule>();
    16.         shipControlModule.callbackOnHit += callbackOnHit;
    17.     }
    18.  
    19.     private void callbackOnHit(int i, int j)
    20.     {
    21.         LastDamage = Time.time;
    22.     }
    23.  
    24.     private void Update()
    25.     {
    26.         if(shipControlModule.shipInstance.localisedDamageRegionList.Count > 0
    27.             && CanRecharge())
    28.         {
    29.             foreach (var r in shipControlModule.shipInstance.localisedDamageRegionList)
    30.             {
    31.                 if(r.ShieldHealth < r.shieldingAmount)
    32.                 {
    33.                     r.ShieldHealth += ShieldPerSecond * Time.deltaTime;
    34.                 }
    35.             }
    36.         }
    37.         else
    38.         {
    39.             if(shipControlModule.shipInstance.mainDamageRegion.ShieldHealth
    40.                 < shipControlModule.shipInstance.mainDamageRegion.shieldingAmount
    41.                 && CanRecharge())
    42.             {
    43.                 shipControlModule.shipInstance.mainDamageRegion.ShieldHealth += ShieldPerSecond * Time.deltaTime;
    44.             }
    45.         }
    46.     }
    47.  
    48.     private bool CanRecharge()
    49.     {
    50.         if(LastDamage + ShieldDelay < Time.time)
    51.         {
    52.             return true;
    53.         }
    54.         else
    55.         {
    56.             return false;
    57.         }
    58.            
    59.     }
    60. }
    61.  
     
  13. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    Sci-Fi Ship Controller v1.1.2 Beta 1g

    This patch is now available for beta program members.

    [NEW] Ship API - SetWeaponFireDirection method added to provide easier changes at runtime
    [FIXED] PlayerInputModule - Add Events can lose connection to Input Action asset
    [FIXED] Ship AI - maxSpeed is not honoured correctly

    Existing customers can join our Beta Program by private messaging us your name, email address and Unity invoice number.
     
  14. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    Pretty please change = to += on
    shipControlModule.shipInstance.callbackOnDamage = ShipHealthUpdated;
    on SimpleShowShipHealth.
    Lost couple hours looking why delegate is not called when tryed to understand why damage event not working for recharge shields.
    Updated shield recharge script, still low-performance
    Code (CSharp):
    1. using SciFiShipController;
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class ShipShieldHelper : MonoBehaviour
    8. {
    9.     public float ShieldDelay = 2f;
    10.     public float ShieldPerSecond = 1f;
    11.     private float LastDamage;
    12.     private ShipControlModule shipControlModule;
    13.     private float lastHealth;
    14.     private float lastShield;
    15.  
    16.  
    17.     private void Awake()
    18.     {
    19.         shipControlModule = GetComponent<ShipControlModule>();
    20.         shipControlModule.shipInstance.callbackOnDamage += ShipDamaged;
    21.     }
    22.  
    23.     private void ShipDamaged(float mainDamageRegionHealth)
    24.     {
    25.         LastDamage = Time.time;
    26.     }
    27.  
    28.     private void Update()
    29.     {
    30.         if (shipControlModule.shipInstance.localisedDamageRegionList.Count > 0
    31.             && CanRecharge())
    32.         {
    33.             foreach (var r in shipControlModule.shipInstance.localisedDamageRegionList)
    34.             {
    35.                 if(r.ShieldHealth < r.shieldingAmount)
    36.                 {
    37.                     r.ShieldHealth += ShieldPerSecond * Time.deltaTime;
    38.                 }
    39.             }
    40.         }
    41.         else
    42.         {
    43.             if(shipControlModule.shipInstance.mainDamageRegion.ShieldHealth
    44.                 < shipControlModule.shipInstance.mainDamageRegion.shieldingAmount
    45.                 && CanRecharge())
    46.             {
    47.                 shipControlModule.shipInstance.mainDamageRegion.ShieldHealth += ShieldPerSecond * Time.deltaTime;
    48.             }
    49.         }
    50.         Debug.Log("Shield " + shipControlModule.shipInstance.mainDamageRegion.ShieldHealth);
    51.     }
    52.  
    53.     private bool CanRecharge()
    54.     {
    55.         if(LastDamage + ShieldDelay < Time.time)
    56.         {
    57.             return true;
    58.         }
    59.         else
    60.         {
    61.             return false;
    62.         }
    63.          
    64.     }
    65. }
    66.  
     
  15. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    By design we are using delegates rather than events. So the invoke is calling the method name. See the example scripts in the demos folder.
     
  16. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    We use a single callback rather than event subscriptions for performance reasons. If you need to do multiple things do it in the one callback method. Although, we'd recommend you keep those "things" as lightweight as possible to avoid performance issues.

    One of the key objectives of SSC is to be high performance with a low overhead to people's games. We do a significant amount of performance testing to achieve this.
     
    Silvermurk likes this.
  17. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    Okay, got it. Thanks for your work and performance of asset as light as it is)
     
    Last edited: Feb 3, 2020
  18. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    Each frame weapon ajustment addon for beta
    Method works perfect and no longer need to modify ship.cs, thanks for adding it.

    Code (CSharp):
    1. using SciFiShipController;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using UnityEngine;
    6.  
    7. public class WeaponFireDirectionHelper : MonoBehaviour
    8. {
    9.     ShipControlModule shipControlModule;
    10.  
    11.     private void Awake()
    12.     {
    13.         shipControlModule = GetComponent<ShipControlModule>();
    14.     }
    15.  
    16.     private void Update()
    17.     {
    18.         if(shipControlModule && shipControlModule.isActiveAndEnabled && shipControlModule.IsInitialised)
    19.         {
    20.             GetFireDirection();
    21.         }
    22.     }
    23.  
    24.     private void GetFireDirection()
    25.     {
    26.         RaycastHit hit;
    27.         Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0)), out hit, 1000f);
    28.  
    29.         if (hit.collider != null)
    30.         {
    31.             Debug.DrawRay(shipControlModule.shipInstance.RigidbodyPosition, hit.point, Color.red, 0.5f);
    32.  
    33.             for (int i = 0; i < shipControlModule.shipInstance.weaponList.Count; i++)
    34.             {
    35.                 if(shipControlModule.shipInstance.weaponList[i].weaponType == Weapon.WeaponType.FixedProjectile)
    36.                 {
    37.                     shipControlModule.shipInstance.SetWeaponFireDirection(i, hit.point);
    38.                 }
    39.             }
    40.         }
    41.     }
    42. }
    43.  
     
    sstrong likes this.
  19. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    Another bug-looking thing.
    If you use AIInput`s MoveTo(Vector3) ship continues to move by very little after it is stopped.
    Wich is not fixed by either disableing and enableing a controller nither by setting RigidBody Velocity\AngularVelocity to zero.
    Code (CSharp):
    1.        
    2.         if (currentStateID == AIState.moveToStateID)
    3.         {
    4.             if(Vector3.Distance(transform.position, shipAIInputModule.GetTargetPosition()) < ArriveDistance)
    5.             {
    6.                 shipAIInputModule.SetState(AIState.idleStateID);
    7.  
    8.                 if (shipControlModule.IsInitialised)
    9.                 {
    10.                     shipControlModule.DisableShip(false);
    11.                     shipControlModule.EnableShip(false, true);
    12.                 }
    13.             }
    14.         }
    Repo:
     
    Last edited: Feb 3, 2020
  20. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    Can you send us a prefab of your AI ship setup so that we can test?

    What happens if you set gravity acceleration to 0? If the ship starts in the idleStateID state, does it creep forwards?

    What does Debug Mode show in the Ship AI Input Module in the editor at runtime when in the idle state?
     
    Last edited: Feb 4, 2020
  21. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    It reproduces with ones you have in demo - Explorer Ship NPC (Arcade)

    Nope - on Instantiating it works fine standing in place, untill you issue a move order by AI.
    Works alike with 9.81 acceleration on -1 Y, also with 0 acceleration if you disable gravity project-wide.
    And even with enabled acceleration and disabled gravity.

    It will stop "floating" after couple of minutes and will come to semi-stop at local velocity inbetween 0.1 and -.01 and will "float" around +=0.5 units on Y axis:
    upload_2020-2-4_13-16-56.png

    Steps to reproduce on your side:
    Drop Explorer Ship NPC (Arcade) to empty scene, add ShipAiInput module to it, and script listed below([Button] attributes are for Odin inspector for invokeing methods from editor)
    Then just send it flying somewhere and watch it creep after stop for some minutes.

    Code (CSharp):
    1. using SciFiShipController;
    2. using Sirenix.OdinInspector;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Linq;
    6. using UnityEngine;
    7. using static SciFiShipController.Weapon;
    8.  
    9. public class ShipAiHelper : MonoBehaviour
    10. {
    11.     [Range(5f, 100f)]
    12.     public float ArriveDistance = 25f;
    13.     public bool UseTurrets = true;
    14.  
    15.     private ShipControlModule shipControlModule;
    16.     private ShipAIInputModule shipAIInputModule;
    17.     private ShipAiTurretHelper shipAiTurretHelper;
    18.     void Awake()
    19.     {
    20.         shipAIInputModule = GetComponent<ShipAIInputModule>();
    21.         shipControlModule = GetComponent<ShipControlModule>();
    22.         shipAiTurretHelper = GetComponent<ShipAiTurretHelper>();
    23.  
    24.         shipAIInputModule.Initialise();
    25.         shipAIInputModule.SetState(AIState.idleStateID);
    26.     }
    27.  
    28.     private void Update()
    29.     {
    30.         if (shipAiTurretHelper
    31.             && shipControlModule.shipInstance.weaponList != null
    32.             && shipControlModule.shipInstance.weaponList.Count > 0
    33.             && UseTurrets)
    34.         {
    35.             var weapons = shipControlModule.shipInstance.weaponList
    36.                 .Where(x => x.weaponType == WeaponType.TurretProjectile);
    37.  
    38.             foreach(var weapon in weapons)
    39.             {
    40.                 shipControlModule.shipInstance.SetWeaponTarget(weapon.name, shipAiTurretHelper.Target);
    41.             }
    42.         }
    43.  
    44.         int currentStateID = shipAIInputModule.GetState();
    45.        
    46.         if (currentStateID == AIState.moveToStateID)
    47.         {
    48.             if(Vector3.Distance(transform.position, shipAIInputModule.GetTargetPosition()) < ArriveDistance)
    49.             {
    50.                 shipAIInputModule.SetState(AIState.idleStateID);
    51.  
    52.                 if (shipControlModule.IsInitialised)
    53.                 {
    54.                     StopEnable();
    55.                     StopPhysics();
    56.                 }
    57.             }
    58.         }
    59.     }
    60.  
    61.     [Button]
    62.     public void MoveTo(Vector3 Position)
    63.     {
    64.         shipAIInputModule.SetState(AIState.moveToStateID);
    65.         shipAIInputModule.AssignTargetPosition(Position);
    66.     }
    67.  
    68.     [Button]
    69.     public void StopPhysics()
    70.     {
    71.         shipControlModule.ShipRigidbody.velocity = Vector3.zero;
    72.         shipControlModule.ShipRigidbody.angularVelocity = Vector3.zero;
    73.     }
    74.  
    75.     [Button]
    76.     public void StopEnable()
    77.     {
    78.         shipControlModule.DisableShip(false);
    79.         shipControlModule.EnableShip(false, true);
    80.     }
    81. }
    82.  
     
  22. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    Great, thanks for the repro - we'll check it out.
     
    Silvermurk likes this.
  23. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    Sci-Fi Ship Controller v1.1.1 Beta 2d or newer contains the following fix:

    [FIXED] Ship AI - ship's position may become unstable after EnableShip() and velocity is reset/changed.

    Here is some sample code to check. As this is a physically-based controller you may still get some small amounts of movement but it should be very small.

    If you need the ship to be absolutely stationary (very arcade-like, don't call EnableShip again until you want it to move).

    Code (CSharp):
    1. using UnityEngine;
    2. using SciFiShipController;
    3.  
    4. /// <summary>
    5. /// Test script to attach to an AI Ship in the scene.
    6. /// Assumes ShipControlModule and ShipAIInputModule have
    7. /// InitialiseOnAwake enabled.
    8. /// </summary>
    9. public class TestMoveAI : MonoBehaviour
    10. {
    11.     public GameObject target;
    12.     public float ArriveDistance = 10f;
    13.     ShipAIInputModule shipAI = null;
    14.     ShipControlModule shipControlModule = null;
    15.  
    16.     // Use this for initialization
    17.     void Start ()
    18.     {
    19.         shipAI = GetComponent<ShipAIInputModule>();
    20.         if (shipAI != null && target != null)
    21.         {
    22.             shipControlModule = GetComponent<ShipControlModule>();
    23.             shipAI.AssignTargetPosition(target.transform.position);
    24.             shipAI.SetState(AIState.moveToStateID);
    25.         }
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update ()
    30.     {
    31.         if (shipAI.IsInitialised && shipAI.GetState() == AIState.moveToStateID)
    32.         {
    33.             // NOT PERFORMANCE OPTIMISED
    34.             if (Vector3.Distance(transform.position, shipAI.GetTargetPosition()) < ArriveDistance)
    35.             {
    36.                 shipAI.SetState(AIState.idleStateID);
    37.  
    38.                 if (shipControlModule.IsInitialised)
    39.                 {
    40.                     // Bring ship to a rapid stop
    41.                     shipControlModule.DisableShip(false);
    42.  
    43.                     // [Optionally] set gravity to zero while stationary
    44.                     shipControlModule.shipInstance.gravitationalAcceleration = 0f;
    45.  
    46.                     // Re-enable ship but set velocity to 0
    47.                     shipControlModule.EnableShip(false, true);
    48.                 }
    49.             }
    50.         }
    51.     }
    52. }
    53.  
     
    Silvermurk likes this.
  24. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    Also would be very cool so see some kind of targeting UI for this controller)
     
  25. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    We've seen other assets do this but you would end up with all games looking alike. Most people want to create their own (unique) UI. However, we are building a radar system which will be able to supply data to your UI. We "might" add a simple mini-map and radar screen as a demo. However, we suspect no-one will actually use it in a real game.

    An example are our "demo" placeholder ships. Great for testing but not so good if you want your own [unique] game.
     
    Last edited: Feb 5, 2020
  26. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    I meant add a sample of UI as en example) But you are right. If it would be game-ready - games would look same.
     
    sstrong likes this.
  27. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    Number of issies starts to make me sad (

    Something is very wrong with projectile physics on stationary objects.
    Reproduction is simple - slow down projectile speed (i used demo peojectile1) to 100`s u\s.
    point it (doesnt matter fixed fire point or turret) towrds something that has a ShipControlModule on it and won`t move at all.
    After 2-3 hits projectiles will stop to register hits untill it either moves or will be forcebly moved by major collision that will move target more than 2-3 units in space.

    Full repo.
     
  28. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    We couldn't repro when projectile was moving at 100s m/s. However, when projectiles moved slowly (in the visible spectrum) we started to see some issue. It seems to be very time dependent. For some values like 25 (in our scenario) it worked without fault, while other values like 12 m/s (very slow) or even 50 m/s we saw some misses.

    Also, don't forget, when slowing down projectiles, you might need to increase the Despawn Time on the projectiles by a second or two.

    For us, when the issue occurred, we didn't need to move anything. After a few seconds it started to work again. Which indicates it could be a timing problem.

    Thanks for reporting this (you're the first to see it :))

    The issues seems to appear when V Sync is enabled.
     
    Last edited: Feb 6, 2020
  29. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    Some further investigation reveal it might be an issue with the pooling system. If you turn off pooling on that projectile, do you still get this issue?
     
  30. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    We have a fix for the projectile pooling system (physics calculations are fine).

    The fix is in 1.1.2 Beta 2g or newer.

    [FIXED] Projectiles - when pooling is enabled, projectiles may get prematurely de-spawned
     
    Silvermurk likes this.
  31. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    Thanks for fast fix:)
    And can you please make a bool variable or point me to a line in code wich will stop camera from reversing it`s position when you fly backwards?:)
     
  32. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    If I add a reverse thruster to the sample Explorer (Arcade) ship and hook it up to the PlayerCamera prefab with the following settings the camera still faces forwards when reversing. I can even "Lock To Target Pos" if need be.

    upload_2020-2-6_15-43-44.png

    What settings are you using?

    BTW you don't need to use our Ship Camera Module. It will also work with the standard Unity camera setup.
     
    Last edited: Feb 6, 2020
  33. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    Camera module has very nice features - smooth movement and ability to fly upside down.
    Yeap, it occurs only when it is set to Follow Target Velocity

    What i meant is this:
     
  34. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    Try Camera Rotation Mode: Follow Target Rotation with "Orient Updates" turned off.

    You may get a slight movement as you go vertical but you shouldn't get flipped like with Follow Velocity.
     
  35. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    Switching to Follow Target Rotation fixed this, thanks)
     
    sstrong likes this.
  36. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    Got a flanky bug with ReletiveTorque:
    If you pause the game with TIme.Scale = 0 and then return it to 1 - sometimts all active ships report that
    Code (CSharp):
    1. rigidbody.torque assign attempt for 'TestShip' is not valid. Input torque is { NaN, NaN, NaN }.
    2. UnityEngine.Rigidbody:AddRelativeTorque(Vector3)
    3. SciFiShipController.ShipControlModule:FixedUpdate() (at Assets/SciFiShipController/Scripts/Physics/Behaviours/ShipControlModule.cs:423)
    4.  
     
  37. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    Small showcase of modded controller:


    Also inventory system with stats:
    upload_2020-2-8_0-30-5.png
     
    sstrong likes this.
  38. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    We've seen this behaviour in other games we've worked on. Just setting the timescale to 0 in game pause mode is usually not sufficient. It can also lead to issues with any code that calculates results over multiple frames (which some internal Unity code does).

    A better approach (IMHO) is to disable all systems before setting timescale to 0, then reenabling them after reverting back. From memory, the order you do this is important.

    In Sci-Fi Ship Controller, call the various DisableXXX / EnableXXX methods supplied (e.g. DisableShip, DisableCamera). A quick and dirty method is also to disable/enable MonoBehaviours under a parent GameObject - although, where possible use the methods supplied.
     
  39. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    Will add your advice to TODO list:)
    SettingTime.TimeScale to 0.01 instead of 0 works fine for now.
     
  40. Dave_2000

    Dave_2000

    Joined:
    Oct 17, 2015
    Posts:
    52
    AI Post #3: Idle and Dogfight Default States

    AI Post #1: https://forum.unity.com/threads/rel...-playable-demos-ai.594448/page-2#post-5368236
    AI Post #2: https://forum.unity.com/threads/rel...-playable-demos-ai.594448/page-2#post-5399064

    As mentioned in the last AI post, in this post I’ll be talking about some of the default states that come with Sci-Fi Ship Controller (specifically, the idle and dogfight states).

    The first of these, the idle state, is useful when you want the ship to not do anything (e.g. if it had landed in a hangar and was waiting for permission to leave). You can set the state of an AI ship to the idle state using the following code:

    Code (CSharp):
    1. // Set the AI's current state to the "idle" state
    2. shipAIInputModule.SetState(AIState.idleStateID);
    There are no inputs that can be set for the idle state – the ship will always simply come to a stop and remain stationary.

    The second default state, the dogfight state, is useful when you want a ship to attack another ship. While in the dogfight state, the ship will pursue the target ship and fire at it. If the target ship gets behind it, it will attempt to evade it, making sure to remain outside of its immediate targeting area. While it is doing this, it will also attempt to avoid any obstacles that appear in its way. You can set the state of an AI ship to the dogfight state using the following code:

    Code (CSharp):
    1. // Set the AI's current state to the "dogfight" state
    2. shipAIInputModule.SetState(AIState.dogfightStateID);
    The dogfight state takes one input: The target ship (which it will attack). You can set the target ship using the following code:

    Code (CSharp):
    1. // Set the target ship to the enemy ship
    2. shipAIInputModule.AssignTargetShip(enemyShip);
    In the next post, I’ll be explaining how you can use the last default state, move to.
     
    tgamorris76 and Silvermurk like this.
  41. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    Good everning, enjoying asset, but had to modify it in couple of places, so came to ask:
    I made my own version of a locking system\radar but now i need to modify ship.cs for damage types and line of sight data for autofire turrets.
    Would it be too hard for you to add an interface or an API to feed a Line Of Sight data to turrets from outside ship.cs?
    And\or make a public bool variable to ignore own projectiles?
    Thanks in advance)
     
  42. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    Line of Sight for auto-fire turrets is already in the latest beta (1.1.2 Beta 2m).

    [NEW] Auto-fire turrets can optionally check line-of-sight

    We have a pretty comprehensive damage system. Maybe you could explain your scenario so that we can understand better what you're doing.
     
  43. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    LoS works great, thanks a bunch!

    I needed to split damage between types, like termal, explosive and such. Wich resulted changes in all related methods in chip.cs.
    Commented them all out for now, will probably add those to 1.1.2 when it`ll be released not to cherry-pick those after each patch-update on beta)
     
  44. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    Can you explain a bit more? e.g. when xyz happens I'd like to do this, but when this other things happens I'd like to do this other thing.

    Are you already using the shipControlModule callbackOnDamage delegate to get notified when damage occurs? You should never need to modify ship.cs.
     
  45. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    I need to modify damage done before it is actually calculated.
    To explain in details:
    I need projectile to do different amount of damage based on its damage type and target resistances.
    As an example projectile has 10 damage of termal type.
    Target ship has termal resistance of 0.5 -> it should recive 10*0.5 = 5 damage.
    Implementing this requires modification of ship and projectle scripts at the moment.
    Callback happens after damage is applyed, as far as i understand.
     
  46. Dave_2000

    Dave_2000

    Joined:
    Oct 17, 2015
    Posts:
    52
    We're investigating the possibility of adding a callback before damage is applied, so that you could modify the damage amount before it is applied to the ship. If we were to add a callback, what data would you like to receive from it?
     
  47. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    Announcement - Coming in 1.1.2 is our new Radar API

    This feature will allow you to:
    • Quickly configure space craft to be detected by radar
    • Add stationary locations to the radar system
    • Add any gameobject via the API
    • Query the radar with a few lines of code
    • Allow the radar to 'move' with a ship or gameobject
    • Use our UI and mini-map or create your own
    • Get nearby hostile ships, locations or gameobjects
    Shortly a beta version will be available for customers to test, provide feedback and influence capability.
     
  48. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    To make it most generic i think it would be nice to have:
    - Vector3 from hit.point from projectile hit for a effects\decals,
    - float Damage with damage amount.
    - string for all generic stuff that people may need, damage tyes included.
    - A way to give new damage amount back to ship if damage value was changed during callback.

    Posseble additions-
    - Built in damage types Em\Termal\Kinetic\Explosive
    - Vector 3 force applyed from collision with projectile

    Suplse it should look something like:
    Projectile hits for 10 Em damage
    Callback is called for BeforeDamageDealt
    Stuff happens based on damage and it`s type or other things (like resistancees applyed)
    New damage amount is returned to ship,cs wich is applyed as normal damage as it is now.

    UPD
    And can you pretty please make some external general damage method that can damage not-ship based things? Otherwise we`ll have to modify projectile script by adding those for asteroids and space dabree.
     
    Last edited: Feb 11, 2020
  49. Silvermurk

    Silvermurk

    Joined:
    Apr 29, 2016
    Posts:
    164
    Another UPD - not using RigidBody and Collider makes makeing projectile a missile with guidance a very difficult task.
    Most things require one or another.
    Would be glad to see one included in asset:)
     
  50. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,251
    Guided projectiles are on our roadmap. We test with thousands of projectiles in the scene at any one time - so any solution needs to be highly scalable (think of an intense space battle).

    Our current DOTS / ECS projectile solution scales to very high numbers. We have also been testing with the new Physics DOTS-based system. This supports DOTS-based physics colliders however Unity still have this under development. You may have noticed the following addition in the Sci-Fi Ship Controller v1.1.1 release:

    [NEW] Projectiles with DOTS enabled, can hit Entities with a PhysicsCollider

    Typically, you don't want to have thousands of colliders all flying around colliding with other things as Unity needs to do A LOT of physics-based calculations which will hurt performance.