Skip to content

Commit f052224

Browse files
authored
Merge pull request #1365 from thestonefox/fix/button-movement-on-enable
Fix/button movement on enable - fixes #1333
2 parents 7cd0d59 + 915fa2f commit f052224

File tree

1 file changed

+129
-103
lines changed

1 file changed

+129
-103
lines changed

Assets/VRTK/Scripts/Controls/3D/VRTK_Button.cs

Lines changed: 129 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -113,30 +113,37 @@ protected override void OnDrawGizmos()
113113
Gizmos.DrawLine(bounds.center, bounds.center + activationDir);
114114
}
115115

116-
protected override void InitRequiredComponents()
116+
protected virtual void SetupCollider()
117117
{
118-
restingPosition = transform.position;
119-
120-
if (!GetComponent<Collider>())
118+
if (GetComponent<Collider>() == null)
121119
{
122120
gameObject.AddComponent<BoxCollider>();
123121
}
122+
}
124123

124+
protected virtual void SetupRigidbody()
125+
{
125126
buttonRigidbody = GetComponent<Rigidbody>();
126127
if (buttonRigidbody == null)
127128
{
128129
buttonRigidbody = gameObject.AddComponent<Rigidbody>();
129130
}
130131
buttonRigidbody.isKinematic = false;
131132
buttonRigidbody.useGravity = false;
133+
}
132134

135+
protected virtual void SetupConstantForce()
136+
{
133137
buttonForce = GetComponent<ConstantForce>();
134138
if (buttonForce == null)
135139
{
136140
buttonForce = gameObject.AddComponent<ConstantForce>();
137141
}
142+
}
138143

139-
if (connectedTo)
144+
protected virtual void SetupConnectedTo()
145+
{
146+
if (connectedTo != null)
140147
{
141148
Rigidbody connectedToRigidbody = connectedTo.GetComponent<Rigidbody>();
142149
if (connectedToRigidbody == null)
@@ -147,6 +154,115 @@ protected override void InitRequiredComponents()
147154
}
148155
}
149156

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+
150266
protected override bool DetectSetup()
151267
{
152268
finalDirection = (direction == ButtonDirection.autodetect ? DetectDirection() : direction);
@@ -155,109 +271,19 @@ protected override bool DetectSetup()
155271
activationDir = Vector3.zero;
156272
return false;
157273
}
158-
if (direction != ButtonDirection.autodetect)
159-
{
160-
activationDir = CalculateActivationDir();
161-
}
162274

163-
if (buttonForce)
275+
activationDir = (direction != ButtonDirection.autodetect ? CalculateActivationDir() : activationDir);
276+
277+
if (buttonForce != null)
164278
{
165279
buttonForce.force = GetForceVector();
166280
}
167281

168282
if (Application.isPlaying)
169283
{
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();
261287
}
262288

263289
return true;
@@ -294,7 +320,7 @@ protected override void HandleUpdate()
294320
}
295321
else
296322
{
297-
if(oldState == 1)
323+
if (oldState == 1)
298324
{
299325
value = 0;
300326
OnReleased(SetControlEvent());
@@ -305,7 +331,7 @@ protected override void HandleUpdate()
305331
protected virtual void FixedUpdate()
306332
{
307333
// 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)
309335
{
310336
restingPosition = transform.position;
311337
}

0 commit comments

Comments
 (0)