@@ -135,12 +135,13 @@ private void FixedUpdate()
135
135
///if above water, trace down to find water
136
136
//if touching/in water trace down from diameterx2 above last water height at current xz to find water
137
137
RaycastHit hit ;
138
+ FloatTouchWaterPoint [ currentfloatpoint ] = FloatLastRayHitHeight [ currentfloatpoint ] + FloatDiameter + Waves . y ;
138
139
if ( FloatTouchWaterPoint [ currentfloatpoint ] > TopOfFloat . y )
139
140
{
140
141
//Touching or under water
141
142
if ( DoOnLand || ! HitLandLast [ currentfloatpoint ] )
142
143
{
143
- FloatDepth [ currentfloatpoint ] = FloatTouchWaterPoint [ currentfloatpoint ] - TopOfFloat . y + Waves . y ;
144
+ FloatDepth [ currentfloatpoint ] = FloatTouchWaterPoint [ currentfloatpoint ] - TopOfFloat . y ;
144
145
float CompressionDifference = ( ( FloatDepth [ currentfloatpoint ] - FloatDepthLastFrame [ currentfloatpoint ] ) ) ;
145
146
if ( CompressionDifference > 0 )
146
147
{ CompressionDifference = Mathf . Min ( CompressionDifference * Compressing , MaxCompressingForce ) ; }
@@ -150,14 +151,18 @@ private void FixedUpdate()
150
151
}
151
152
FloatDepthLastFrame [ currentfloatpoint ] = FloatDepth [ currentfloatpoint ] ;
152
153
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 ) )
155
158
{
156
159
if ( DoOnLand || hit . collider . isTrigger )
157
- { FloatTouchWaterPoint [ currentfloatpoint ] = hit . point . y + FloatDiameter ; }
160
+ { FloatLastRayHitHeight [ currentfloatpoint ] = hit . point . y ; ; }
158
161
}
159
162
else
160
- { FloatTouchWaterPoint [ currentfloatpoint ] = float . MinValue ; }
163
+ {
164
+ FloatLastRayHitHeight [ currentfloatpoint ] = float . MinValue ;
165
+ }
161
166
}
162
167
}
163
168
else
@@ -169,6 +174,8 @@ private void FixedUpdate()
169
174
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
170
175
{ FloatTouchWaterPoint [ currentfloatpoint ] = float . MinValue ; }
171
176
//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 ) ;
172
179
if ( Physics . Raycast ( TopOfFloat , - Vector3 . up , out hit , 35 , FloatLayers , QueryTriggerInteraction . Collide ) )
173
180
{
174
181
FloatTouchWaterPoint [ currentfloatpoint ] = hit . point . y + FloatDiameter ;
@@ -192,39 +199,41 @@ private void FixedUpdate()
192
199
VehicleRigidbody . AddTorque ( - VehicleRigidbody . angularVelocity * DepthMaxd * WaterRotDrag ) ;
193
200
VehicleRigidbody . AddForce ( - VehicleRigidbody . velocity * DepthMaxd * WaterVelDrag ) ;
194
201
if ( SAVControl && ! HoverBike ) { SAVControl . SetProgramVariable ( "Floating" , true ) ; }
195
- }
196
- else
197
- { if ( SAVControl && ! HoverBike ) { SAVControl . SetProgramVariable ( "Floating" , false ) ; } }
198
202
199
- Vector3 right = VehicleTransform . right ;
200
- Vector3 forward = VehicleTransform . forward ;
201
203
202
- float sidespeed = Vector3 . Dot ( Vel , right ) ;
203
- float forwardspeed = Vector3 . Dot ( Vel , forward ) ;
204
+ Vector3 right = VehicleTransform . right ;
205
+ Vector3 forward = VehicleTransform . forward ;
204
206
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 )
211
211
{
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 ) ;
213
227
}
214
228
else
215
229
{
216
- right = Vector3 . zero ;
230
+ VehicleRigidbody . AddForceAtPosition ( right * - sidespeed * WaterSidewaysDrag * DepthMaxd , FloatPoints [ currentfloatpoint ] . position , ForceMode . Acceleration ) ;
217
231
}
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
+
222
234
}
223
235
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 ) ; } }
228
237
229
238
currentfloatpoint ++ ;
230
239
if ( currentfloatpoint == FPLength ) { currentfloatpoint = 0 ; }
0 commit comments