Skip to content

Commit 89d0840

Browse files
committed
fix floatscript more
1 parent f18ed0a commit 89d0840

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

Scripts/SaccAirVehicle/SAV_Extensions/SAV_FloatScript.cs

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,13 @@ private void FixedUpdate()
135135
///if above water, trace down to find water
136136
//if touching/in water trace down from diameterx2 above last water height at current xz to find water
137137
RaycastHit hit;
138+
FloatTouchWaterPoint[currentfloatpoint] = FloatLastRayHitHeight[currentfloatpoint] + FloatDiameter + Waves.y;
138139
if (FloatTouchWaterPoint[currentfloatpoint] > TopOfFloat.y)
139140
{
140141
//Touching or under water
141142
if (DoOnLand || !HitLandLast[currentfloatpoint])
142143
{
143-
FloatDepth[currentfloatpoint] = FloatTouchWaterPoint[currentfloatpoint] - TopOfFloat.y + Waves.y;
144+
FloatDepth[currentfloatpoint] = FloatTouchWaterPoint[currentfloatpoint] - TopOfFloat.y;
144145
float CompressionDifference = ((FloatDepth[currentfloatpoint] - FloatDepthLastFrame[currentfloatpoint]));
145146
if (CompressionDifference > 0)
146147
{ CompressionDifference = Mathf.Min(CompressionDifference * Compressing, MaxCompressingForce); }
@@ -150,14 +151,18 @@ private void FixedUpdate()
150151
}
151152
FloatDepthLastFrame[currentfloatpoint] = FloatDepth[currentfloatpoint];
152153
FloatPointForce[currentfloatpoint] = Vector3.up * (((Mathf.Min(FloatDepth[currentfloatpoint], MaxDepthForce) * FloatForce) + CompressionDifference));
153-
Vector3 checksurface = new Vector3(TopOfFloat.x, FloatTouchWaterPoint[currentfloatpoint], TopOfFloat.z);
154-
if (Physics.Raycast(checksurface, -Vector3.up, out hit, FloatDiameter * 2, FloatLayers, QueryTriggerInteraction.Collide))
154+
//float is potentially below the top of the trigger, so fire a raycast from above the last known trigger height to check if it's still there
155+
//the '+10': larger number means less chance of error if moving faster on a sloped water trigger, but could cause issues with bridges etc
156+
Vector3 checksurface = new Vector3(TopOfFloat.x, FloatLastRayHitHeight[currentfloatpoint] + 10, TopOfFloat.z);
157+
if (Physics.Raycast(checksurface, -Vector3.up, out hit, 14, FloatLayers, QueryTriggerInteraction.Collide))
155158
{
156159
if (DoOnLand || hit.collider.isTrigger)
157-
{ FloatTouchWaterPoint[currentfloatpoint] = hit.point.y + FloatDiameter; }
160+
{ FloatLastRayHitHeight[currentfloatpoint] = hit.point.y; ; }
158161
}
159162
else
160-
{ FloatTouchWaterPoint[currentfloatpoint] = float.MinValue; }
163+
{
164+
FloatLastRayHitHeight[currentfloatpoint] = float.MinValue;
165+
}
161166
}
162167
}
163168
else
@@ -169,6 +174,8 @@ private void FixedUpdate()
169174
if (Vel.y > 0 || HitLandLast[currentfloatpoint])//only reset water level if moving up (or last hit was land), so things don't break if we go straight from air all the way to under the water
170175
{ FloatTouchWaterPoint[currentfloatpoint] = float.MinValue; }
171176
//Debug.Log(string.Concat(currentfloatpoint.ToString(), ": Air: floatpointforce: ", FloatPointForce[currentfloatpoint].ToString()));
177+
//float could be below the top of the trigger if the waves are big enough, check for water trigger with current position + waveheight
178+
Vector3 checksurface = new Vector3(TopOfFloat.x, TopOfFloat.y + WaveHeight, TopOfFloat.z);
172179
if (Physics.Raycast(TopOfFloat, -Vector3.up, out hit, 35, FloatLayers, QueryTriggerInteraction.Collide))
173180
{
174181
FloatTouchWaterPoint[currentfloatpoint] = hit.point.y + FloatDiameter;
@@ -192,39 +199,41 @@ private void FixedUpdate()
192199
VehicleRigidbody.AddTorque(-VehicleRigidbody.angularVelocity * DepthMaxd * WaterRotDrag);
193200
VehicleRigidbody.AddForce(-VehicleRigidbody.velocity * DepthMaxd * WaterVelDrag);
194201
if (SAVControl && !HoverBike) { SAVControl.SetProgramVariable("Floating", true); }
195-
}
196-
else
197-
{ if (SAVControl && !HoverBike) { SAVControl.SetProgramVariable("Floating", false); } }
198202

199-
Vector3 right = VehicleTransform.right;
200-
Vector3 forward = VehicleTransform.forward;
201203

202-
float sidespeed = Vector3.Dot(Vel, right);
203-
float forwardspeed = Vector3.Dot(Vel, forward);
204+
Vector3 right = VehicleTransform.right;
205+
Vector3 forward = VehicleTransform.forward;
204206

205-
if (HoverBike)
206-
{
207-
Vector3 up = VehicleTransform.up;
208-
float RightY = Mathf.Abs(right.y);
209-
right.y = 0;
210-
if (Vector3.Dot(Vel, -up) > 0)
207+
float sidespeed = Vector3.Dot(Vel, right);
208+
float forwardspeed = Vector3.Dot(Vel, forward);
209+
210+
if (HoverBike)
211211
{
212-
right = right.normalized * (1 + (RightY * HoverBikeTurningStrength));
212+
Vector3 up = VehicleTransform.up;
213+
float RightY = Mathf.Abs(right.y);
214+
right.y = 0;
215+
if (Vector3.Dot(Vel, -up) > 0)
216+
{
217+
right = right.normalized * (1 + (RightY * HoverBikeTurningStrength));
218+
}
219+
else
220+
{
221+
right = Vector3.zero;
222+
}
223+
float BackThrustAmount = -((Vector3.Dot(Vel, forward)) * BackThrustStrength);
224+
if (BackThrustAmount > 0)
225+
{ VehicleRigidbody.AddForce(forward * BackThrustAmount * DepthMaxd * (float)SAVControl.GetProgramVariable("ThrottleInput"), ForceMode.Acceleration); }
226+
VehicleRigidbody.AddForce(right * -sidespeed * WaterSidewaysDrag * DepthMaxd, ForceMode.Acceleration);
213227
}
214228
else
215229
{
216-
right = Vector3.zero;
230+
VehicleRigidbody.AddForceAtPosition(right * -sidespeed * WaterSidewaysDrag * DepthMaxd, FloatPoints[currentfloatpoint].position, ForceMode.Acceleration);
217231
}
218-
float BackThrustAmount = -((Vector3.Dot(Vel, forward)) * BackThrustStrength);
219-
if (BackThrustAmount > 0)
220-
{ VehicleRigidbody.AddForce(forward * BackThrustAmount * DepthMaxd * (float)SAVControl.GetProgramVariable("ThrottleInput"), ForceMode.Acceleration); }
221-
VehicleRigidbody.AddForce(right * -sidespeed * WaterSidewaysDrag * DepthMaxd, ForceMode.Acceleration);
232+
VehicleRigidbody.AddForceAtPosition(forward * -forwardspeed * WaterForwardDrag * DepthMaxd, FloatPoints[currentfloatpoint].position, ForceMode.Acceleration);
233+
222234
}
223235
else
224-
{
225-
VehicleRigidbody.AddForceAtPosition(right * -sidespeed * WaterSidewaysDrag * DepthMaxd, FloatPoints[currentfloatpoint].position, ForceMode.Acceleration);
226-
}
227-
VehicleRigidbody.AddForceAtPosition(forward * -forwardspeed * WaterForwardDrag * DepthMaxd, FloatPoints[currentfloatpoint].position, ForceMode.Acceleration);
236+
{ if (SAVControl && !HoverBike) { SAVControl.SetProgramVariable("Floating", false); } }
228237

229238
currentfloatpoint++;
230239
if (currentfloatpoint == FPLength) { currentfloatpoint = 0; }

0 commit comments

Comments
 (0)