@@ -113,30 +113,37 @@ protected override void OnDrawGizmos()
113
113
Gizmos . DrawLine ( bounds . center , bounds . center + activationDir ) ;
114
114
}
115
115
116
- protected override void InitRequiredComponents ( )
116
+ protected virtual void SetupCollider ( )
117
117
{
118
- restingPosition = transform . position ;
119
-
120
- if ( ! GetComponent < Collider > ( ) )
118
+ if ( GetComponent < Collider > ( ) == null )
121
119
{
122
120
gameObject . AddComponent < BoxCollider > ( ) ;
123
121
}
122
+ }
124
123
124
+ protected virtual void SetupRigidbody ( )
125
+ {
125
126
buttonRigidbody = GetComponent < Rigidbody > ( ) ;
126
127
if ( buttonRigidbody == null )
127
128
{
128
129
buttonRigidbody = gameObject . AddComponent < Rigidbody > ( ) ;
129
130
}
130
131
buttonRigidbody . isKinematic = false ;
131
132
buttonRigidbody . useGravity = false ;
133
+ }
132
134
135
+ protected virtual void SetupConstantForce ( )
136
+ {
133
137
buttonForce = GetComponent < ConstantForce > ( ) ;
134
138
if ( buttonForce == null )
135
139
{
136
140
buttonForce = gameObject . AddComponent < ConstantForce > ( ) ;
137
141
}
142
+ }
138
143
139
- if ( connectedTo )
144
+ protected virtual void SetupConnectedTo ( )
145
+ {
146
+ if ( connectedTo != null )
140
147
{
141
148
Rigidbody connectedToRigidbody = connectedTo . GetComponent < Rigidbody > ( ) ;
142
149
if ( connectedToRigidbody == null )
@@ -147,6 +154,115 @@ protected override void InitRequiredComponents()
147
154
}
148
155
}
149
156
157
+ protected override void InitRequiredComponents ( )
158
+ {
159
+ restingPosition = transform . position ;
160
+
161
+ SetupCollider ( ) ;
162
+ SetupRigidbody ( ) ;
163
+ SetupConstantForce ( ) ;
164
+ SetupConnectedTo ( ) ;
165
+ }
166
+
167
+ protected virtual void DetectJointSetup ( )
168
+ {
169
+ buttonJoint = GetComponent < ConfigurableJoint > ( ) ;
170
+ bool recreate = false ;
171
+ Rigidbody oldBody = null ;
172
+ Vector3 oldAnchor = Vector3 . zero ;
173
+ Vector3 oldAxis = Vector3 . zero ;
174
+
175
+ if ( buttonJoint != null )
176
+ {
177
+ // save old values, needs to be recreated
178
+ oldBody = buttonJoint . connectedBody ;
179
+ oldAnchor = buttonJoint . anchor ;
180
+ oldAxis = buttonJoint . axis ;
181
+ DestroyImmediate ( buttonJoint ) ;
182
+ recreate = true ;
183
+ }
184
+
185
+ // since limit applies to both directions object needs to be moved halfway to activation before adding joint
186
+ transform . position = transform . position + ( ( activationDir . normalized * activationDistance ) * 0.5f ) ;
187
+ buttonJoint = gameObject . AddComponent < ConfigurableJoint > ( ) ;
188
+
189
+ if ( recreate )
190
+ {
191
+ buttonJoint . connectedBody = oldBody ;
192
+ buttonJoint . anchor = oldAnchor ;
193
+ buttonJoint . axis = oldAxis ;
194
+ }
195
+
196
+ buttonJoint . connectedBody = ( connectedTo != null ? connectedTo . GetComponent < Rigidbody > ( ) : buttonJoint . connectedBody ) ;
197
+ buttonJoint . autoConfigureConnectedAnchor = false ;
198
+ }
199
+
200
+ protected virtual void DetectJointLimitsSetup ( )
201
+ {
202
+ SoftJointLimit buttonJointLimits = new SoftJointLimit ( ) ;
203
+ buttonJointLimits . limit = activationDistance * 0.501f ; // set limit to half (since it applies to both directions) and a tiny bit larger since otherwise activation distance might be missed
204
+ buttonJoint . linearLimit = buttonJointLimits ;
205
+
206
+ buttonJoint . angularXMotion = ConfigurableJointMotion . Locked ;
207
+ buttonJoint . angularYMotion = ConfigurableJointMotion . Locked ;
208
+ buttonJoint . angularZMotion = ConfigurableJointMotion . Locked ;
209
+ buttonJoint . xMotion = ConfigurableJointMotion . Locked ;
210
+ buttonJoint . yMotion = ConfigurableJointMotion . Locked ;
211
+ buttonJoint . zMotion = ConfigurableJointMotion . Locked ;
212
+ }
213
+
214
+ protected virtual void DetectJointDirectionSetup ( )
215
+ {
216
+ switch ( finalDirection )
217
+ {
218
+ case ButtonDirection . x :
219
+ case ButtonDirection . negX :
220
+ if ( Mathf . RoundToInt ( Mathf . Abs ( transform . right . x ) ) == 1 )
221
+ {
222
+ buttonJoint . xMotion = ConfigurableJointMotion . Limited ;
223
+ }
224
+ else if ( Mathf . RoundToInt ( Mathf . Abs ( transform . up . x ) ) == 1 )
225
+ {
226
+ buttonJoint . yMotion = ConfigurableJointMotion . Limited ;
227
+ }
228
+ else if ( Mathf . RoundToInt ( Mathf . Abs ( transform . forward . x ) ) == 1 )
229
+ {
230
+ buttonJoint . zMotion = ConfigurableJointMotion . Limited ;
231
+ }
232
+ break ;
233
+ case ButtonDirection . y :
234
+ case ButtonDirection . negY :
235
+ if ( Mathf . RoundToInt ( Mathf . Abs ( transform . right . y ) ) == 1 )
236
+ {
237
+ buttonJoint . xMotion = ConfigurableJointMotion . Limited ;
238
+ }
239
+ else if ( Mathf . RoundToInt ( Mathf . Abs ( transform . up . y ) ) == 1 )
240
+ {
241
+ buttonJoint . yMotion = ConfigurableJointMotion . Limited ;
242
+ }
243
+ else if ( Mathf . RoundToInt ( Mathf . Abs ( transform . forward . y ) ) == 1 )
244
+ {
245
+ buttonJoint . zMotion = ConfigurableJointMotion . Limited ;
246
+ }
247
+ break ;
248
+ case ButtonDirection . z :
249
+ case ButtonDirection . negZ :
250
+ if ( Mathf . RoundToInt ( Mathf . Abs ( transform . right . z ) ) == 1 )
251
+ {
252
+ buttonJoint . xMotion = ConfigurableJointMotion . Limited ;
253
+ }
254
+ else if ( Mathf . RoundToInt ( Mathf . Abs ( transform . up . z ) ) == 1 )
255
+ {
256
+ buttonJoint . yMotion = ConfigurableJointMotion . Limited ;
257
+ }
258
+ else if ( Mathf . RoundToInt ( Mathf . Abs ( transform . forward . z ) ) == 1 )
259
+ {
260
+ buttonJoint . zMotion = ConfigurableJointMotion . Limited ;
261
+ }
262
+ break ;
263
+ }
264
+ }
265
+
150
266
protected override bool DetectSetup ( )
151
267
{
152
268
finalDirection = ( direction == ButtonDirection . autodetect ? DetectDirection ( ) : direction ) ;
@@ -155,109 +271,19 @@ protected override bool DetectSetup()
155
271
activationDir = Vector3 . zero ;
156
272
return false ;
157
273
}
158
- if ( direction != ButtonDirection . autodetect )
159
- {
160
- activationDir = CalculateActivationDir ( ) ;
161
- }
162
274
163
- if ( buttonForce )
275
+ activationDir = ( direction != ButtonDirection . autodetect ? CalculateActivationDir ( ) : activationDir ) ;
276
+
277
+ if ( buttonForce != null )
164
278
{
165
279
buttonForce . force = GetForceVector ( ) ;
166
280
}
167
281
168
282
if ( Application . isPlaying )
169
283
{
170
- buttonJoint = GetComponent < ConfigurableJoint > ( ) ;
171
-
172
- bool recreate = false ;
173
- Rigidbody oldBody = null ;
174
- Vector3 oldAnchor = Vector3 . zero ;
175
- Vector3 oldAxis = Vector3 . zero ;
176
-
177
- if ( buttonJoint )
178
- {
179
- // save old values, needs to be recreated
180
- oldBody = buttonJoint . connectedBody ;
181
- oldAnchor = buttonJoint . anchor ;
182
- oldAxis = buttonJoint . axis ;
183
- DestroyImmediate ( buttonJoint ) ;
184
- recreate = true ;
185
- }
186
-
187
- // since limit applies to both directions object needs to be moved halfway to activation before adding joint
188
- transform . position = transform . position + ( ( activationDir . normalized * activationDistance ) * 0.5f ) ;
189
- buttonJoint = gameObject . AddComponent < ConfigurableJoint > ( ) ;
190
-
191
- if ( recreate )
192
- {
193
- buttonJoint . connectedBody = oldBody ;
194
- buttonJoint . anchor = oldAnchor ;
195
- buttonJoint . axis = oldAxis ;
196
- }
197
- if ( connectedTo )
198
- {
199
- buttonJoint . connectedBody = connectedTo . GetComponent < Rigidbody > ( ) ;
200
- }
201
-
202
- SoftJointLimit buttonJointLimits = new SoftJointLimit ( ) ;
203
- buttonJointLimits . limit = activationDistance * 0.501f ; // set limit to half (since it applies to both directions) and a tiny bit larger since otherwise activation distance might be missed
204
- buttonJoint . linearLimit = buttonJointLimits ;
205
-
206
- buttonJoint . angularXMotion = ConfigurableJointMotion . Locked ;
207
- buttonJoint . angularYMotion = ConfigurableJointMotion . Locked ;
208
- buttonJoint . angularZMotion = ConfigurableJointMotion . Locked ;
209
- buttonJoint . xMotion = ConfigurableJointMotion . Locked ;
210
- buttonJoint . yMotion = ConfigurableJointMotion . Locked ;
211
- buttonJoint . zMotion = ConfigurableJointMotion . Locked ;
212
-
213
- switch ( finalDirection )
214
- {
215
- case ButtonDirection . x :
216
- case ButtonDirection . negX :
217
- if ( Mathf . RoundToInt ( Mathf . Abs ( transform . right . x ) ) == 1 )
218
- {
219
- buttonJoint . xMotion = ConfigurableJointMotion . Limited ;
220
- }
221
- else if ( Mathf . RoundToInt ( Mathf . Abs ( transform . up . x ) ) == 1 )
222
- {
223
- buttonJoint . yMotion = ConfigurableJointMotion . Limited ;
224
- }
225
- else if ( Mathf . RoundToInt ( Mathf . Abs ( transform . forward . x ) ) == 1 )
226
- {
227
- buttonJoint . zMotion = ConfigurableJointMotion . Limited ;
228
- }
229
- break ;
230
- case ButtonDirection . y :
231
- case ButtonDirection . negY :
232
- if ( Mathf . RoundToInt ( Mathf . Abs ( transform . right . y ) ) == 1 )
233
- {
234
- buttonJoint . xMotion = ConfigurableJointMotion . Limited ;
235
- }
236
- else if ( Mathf . RoundToInt ( Mathf . Abs ( transform . up . y ) ) == 1 )
237
- {
238
- buttonJoint . yMotion = ConfigurableJointMotion . Limited ;
239
- }
240
- else if ( Mathf . RoundToInt ( Mathf . Abs ( transform . forward . y ) ) == 1 )
241
- {
242
- buttonJoint . zMotion = ConfigurableJointMotion . Limited ;
243
- }
244
- break ;
245
- case ButtonDirection . z :
246
- case ButtonDirection . negZ :
247
- if ( Mathf . RoundToInt ( Mathf . Abs ( transform . right . z ) ) == 1 )
248
- {
249
- buttonJoint . xMotion = ConfigurableJointMotion . Limited ;
250
- }
251
- else if ( Mathf . RoundToInt ( Mathf . Abs ( transform . up . z ) ) == 1 )
252
- {
253
- buttonJoint . yMotion = ConfigurableJointMotion . Limited ;
254
- }
255
- else if ( Mathf . RoundToInt ( Mathf . Abs ( transform . forward . z ) ) == 1 )
256
- {
257
- buttonJoint . zMotion = ConfigurableJointMotion . Limited ;
258
- }
259
- break ;
260
- }
284
+ DetectJointSetup ( ) ;
285
+ DetectJointLimitsSetup ( ) ;
286
+ DetectJointDirectionSetup ( ) ;
261
287
}
262
288
263
289
return true ;
@@ -294,7 +320,7 @@ protected override void HandleUpdate()
294
320
}
295
321
else
296
322
{
297
- if ( oldState == 1 )
323
+ if ( oldState == 1 )
298
324
{
299
325
value = 0 ;
300
326
OnReleased ( SetControlEvent ( ) ) ;
@@ -305,7 +331,7 @@ protected override void HandleUpdate()
305
331
protected virtual void FixedUpdate ( )
306
332
{
307
333
// update reference position if no force is acting on the button to support scenarios where the button is moved at runtime with a connected body
308
- if ( forceCount == 0 && buttonJoint . connectedBody )
334
+ if ( forceCount == 0 && buttonJoint . connectedBody != null )
309
335
{
310
336
restingPosition = transform . position ;
311
337
}
0 commit comments