Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix boats not receiving input in 2021 #935

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public class BoatAlignNormal : FloatingObjectBase
SampleHeightHelper _sampleHeightHelperLengthwise = new SampleHeightHelper();
SampleFlowHelper _sampleFlowHelper = new SampleFlowHelper();

float _inputForward;
float _inputSideward;

void Start()
{
_rb = GetComponent<Rigidbody>();
Expand All @@ -87,6 +90,37 @@ void Start()
}
}

#if ENABLE_INPUT_SYSTEM
void Update()
{
if (InputSystem.settings.updateMode == InputSettings.UpdateMode.ProcessEventsInDynamicUpdate)
{
UpdateInput();
}
}
#endif

void UpdateInput()
{
#if ENABLE_INPUT_SYSTEM
_inputForward = !Application.isFocused ? 0 :
((Keyboard.current.wKey.isPressed ? 1 : 0) + (Keyboard.current.sKey.isPressed ? -1 : 0));
#else
_inputForward = Input.GetAxis("Vertical");
#endif
var reverseMultiplier = _inputForward < 0f ? -1f : 1f;

#if ENABLE_INPUT_SYSTEM
_inputSideward = !Application.isFocused ? 0 :
((Keyboard.current.aKey.isPressed ? reverseMultiplier * -1f : 0f) +
(Keyboard.current.dKey.isPressed ? reverseMultiplier * 1f : 0f));
#else
_inputSideward =
(Input.GetKey(KeyCode.A) ? reverseMultiplier * -1f : 0f) +
(Input.GetKey(KeyCode.D) ? reverseMultiplier * 1f : 0f);
#endif
}

void FixedUpdate()
{
if (OceanRenderer.Instance == null)
Expand Down Expand Up @@ -146,26 +180,21 @@ void FixedUpdate()
_rb.AddForceAtPosition(transform.right * Vector3.Dot(transform.right, -velocityRelativeToWater) * _dragInWaterRight, forcePosition, ForceMode.Acceleration);
_rb.AddForceAtPosition(transform.forward * Vector3.Dot(transform.forward, -velocityRelativeToWater) * _dragInWaterForward, forcePosition, ForceMode.Acceleration);

float forward = _throttleBias;
#if ENABLE_INPUT_SYSTEM
float rawForward = !Application.isFocused ? 0 : ((Keyboard.current.wKey.isPressed ? 1 : 0) + (Keyboard.current.sKey.isPressed ? -1 : 0));
if (InputSystem.settings.updateMode == InputSettings.UpdateMode.ProcessEventsInFixedUpdate)
{
UpdateInput();
}
#else
float rawForward = Input.GetAxis("Vertical");
UpdateInput();
#endif
if (_playerControlled) forward += rawForward;

float forward = _throttleBias;
if (_playerControlled) forward += _inputForward;
_rb.AddForceAtPosition(transform.forward * _enginePower * forward, forcePosition, ForceMode.Acceleration);

float reverseMultiplier = (rawForward < 0f ? -1f : 1f);
float sideways = _steerBias;
if (_playerControlled) sideways +=
#if ENABLE_INPUT_SYSTEM
!Application.isFocused ? 0 :
((Keyboard.current.aKey.isPressed ? reverseMultiplier * -1f : 0f) +
(Keyboard.current.dKey.isPressed ? reverseMultiplier * 1f : 0f));
#else
(Input.GetKey(KeyCode.A) ? reverseMultiplier * -1f : 0f) +
(Input.GetKey(KeyCode.D) ? reverseMultiplier * 1f : 0f);
#endif
if (_playerControlled) sideways += _inputSideward;
_rb.AddTorque(transform.up * _turnPower * sideways, ForceMode.Acceleration);

FixedUpdateOrientation(normal);
Expand Down
51 changes: 35 additions & 16 deletions crest/Assets/Crest/Crest/Scripts/Interaction/BoatProbes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public class BoatProbes : FloatingObjectBase

SampleFlowHelper _sampleFlowHelper = new SampleFlowHelper();

float _inputForward;
float _inputSideward;

private void Start()
{
_rb = GetComponent<Rigidbody>();
Expand All @@ -92,6 +95,36 @@ private void Start()
_queryResultVels = new Vector3[_forcePoints.Length + 1];
}

#if ENABLE_INPUT_SYSTEM
void Update()
{
if (InputSystem.settings.updateMode == InputSettings.UpdateMode.ProcessEventsInDynamicUpdate)
{
UpdateInput();
}
}
#endif

void UpdateInput()
{
#if ENABLE_INPUT_SYSTEM
_inputForward = !Application.isFocused ? 0 :
((Keyboard.current.wKey.isPressed ? 1 : 0) + (Keyboard.current.sKey.isPressed ? -1 : 0));
#else
_inputForward = Input.GetAxis("Vertical");
#endif

#if ENABLE_INPUT_SYSTEM
_inputSideward = !Application.isFocused ? 0 :
((Keyboard.current.aKey.isPressed ? -1f : 0f) +
(Keyboard.current.dKey.isPressed ? 1f : 0f));
#else
_inputSideward =
(Input.GetKey(KeyCode.A) ? -1f : 0f) +
(Input.GetKey(KeyCode.D) ? 1f : 0f);
#endif
}

void CalcTotalWeight()
{
_totalWeight = 0f;
Expand Down Expand Up @@ -152,25 +185,11 @@ void FixedUpdateEngine()
var forcePosition = _rb.position;

var forward = _engineBias;
if (_playerControlled) forward +=
#if ENABLE_INPUT_SYSTEM
!Application.isFocused ? 0 :
((Keyboard.current.wKey.isPressed ? 1 : 0) + (Keyboard.current.sKey.isPressed ? -1 : 0));
#else
Input.GetAxis("Vertical");
#endif
if (_playerControlled) forward += _inputForward;
_rb.AddForceAtPosition(transform.forward * _enginePower * forward, forcePosition, ForceMode.Acceleration);

var sideways = _turnBias;
if (_playerControlled) sideways +=
#if ENABLE_INPUT_SYSTEM
!Application.isFocused ? 0 :
((Keyboard.current.aKey.isPressed ? -1f : 0f) +
(Keyboard.current.dKey.isPressed ? 1f : 0f));
#else
(Input.GetKey(KeyCode.A) ? -1f : 0f) +
(Input.GetKey(KeyCode.D) ? 1f : 0f);
#endif
if (_playerControlled) sideways += _inputSideward;
var rotVec = transform.up + _turningHeel * transform.forward;
_rb.AddTorque(rotVec * _turnPower * sideways, ForceMode.Acceleration);
}
Expand Down
1 change: 1 addition & 0 deletions docs/about/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Fixed
- Fix FFT spectrum not being editable when time is paused.
- Fix *ShapeFFT* component producing inverted looking waves when enabled in editor play mode.
- Fix SSS colour missing or popping in the distance.
- Fix boat components not responding to user input for 2021.

.. only:: birp

Expand Down