rabidus-test/Assets/Scripts/InputOverride.cs

111 lines
4.2 KiB
C#
Raw Permalink Normal View History

2023-07-24 16:38:13 +03:00
using UnityEngine;
using UnityEngine.InputSystem;
// Sci-Fi Ship Controller. Copyright (c) 2018-2023 SCSM Pty Ltd. All rights reserved.
namespace SciFiShipController
{
public class InputOverride : MonoBehaviour
{
#region Public Variables
public float YawSmooth = 1;
public float PitchSmooth = 1;
public float RollSmooth = 1;
public bool InverseYaw = true;
public InputActionReference LeftTrigger;
public InputActionReference RightTrigger;
public float SpeedInput;
#endregion
#region Private Variables
private PlayerInputModule playerInputModule = null;
private ShipControlModule shipControlModule = null;
private bool isInitialised = false;
private ShipInput shipInput = null;
#endregion
#region Initialisation Methods
// Start is called before the first frame update
void Start()
{
playerInputModule = GetComponent<PlayerInputModule>();
if (playerInputModule != null && playerInputModule.IsInitialised)
{
shipControlModule = playerInputModule.GetShipControlModule;
if (shipControlModule == null)
{
#if UNITY_EDITOR
Debug.LogWarning("ERROR: SampleInputOverride - could not get the ShipControlModule. Did you attach the script to the player ship?");
#endif
}
else if (!shipControlModule.IsInitialised)
{
#if UNITY_EDITOR
Debug.LogWarning("ERROR: SampleInputOverride - is Initialise on Awake is enabled on the Physics tab of the ShipControlModule?");
#endif
}
else
{
// Override the Longitudinal (foward/backward) axis
playerInputModule.isHorizontalDataDiscarded = true;
//playerInputModule.isLongitudinalDataDiscarded = true;
// Re-initialise the DiscardData shipInput settings in the PlayerInputModule
// This is required after one or more of the [axis]DataDiscarded values are changed at runtime.
playerInputModule.ReinitialiseDiscardData();
// Create a new instance of the shipInput which we can send each frame to the ship.
shipInput = new ShipInput();
if (shipInput != null)
{
// Start by disabling everything. This helps to future-proof the code.
// We'll be telling the Ship you can discard anything else that we don't enable below.
shipInput.DisableAllData();
// When we send data, we will tell the ship we'll be sending Longitudinal data only.
shipInput.isHorizontalDataEnabled = true;
//shipInput.isLongitudinalDataEnabled = true;
isInitialised = true;
}
}
}
#if UNITY_EDITOR
else
{
Debug.LogWarning("ERROR: SampleInputOverride - did you forget to attach to the PlayerInputModule? Also check if Initialise on Awake is enabled.");
}
#endif
}
#endregion
#region Update Methods
private void Update()
{
if (isInitialised && shipControlModule.ShipMovementIsEnabled() && shipControlModule.IsActive)
{
SpeedInput = RightTrigger.action.ReadValue<float>() * LeftTrigger.action.ReadValue<float>();
}
else
{
SpeedInput = Mathf.Lerp(SpeedInput, 0, Time.deltaTime);
}
}
public void UpdateYawAxis(float value)
{
if (isInitialised && shipControlModule.ShipMovementIsEnabled())
{
shipInput.horizontal = value * YawSmooth * (InverseYaw ? -1 : 1);
// Tell the ship we want only apply longitudinal input
shipControlModule.SendInput(shipInput);
}
}
#endregion
}
}