Skip to content

Commit d29e85e

Browse files
committed
Fixing arrows not working properly in newer physics sims
1 parent e470717 commit d29e85e

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

Assets/SteamVR/InteractionSystem/Longbow/Scripts/Arrow.cs

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,29 @@ public class Arrow : MonoBehaviour
3737

3838
private GameObject scaleParentObject = null;
3939

40+
private float initialMass;
41+
private float initialDrag;
42+
private float initialAngularDrag;
43+
private RigidbodyInterpolation initialInterpolation;
44+
private CollisionDetectionMode initialCollisionDetection;
45+
private bool initialUseGravity;
4046

41-
//-------------------------------------------------
42-
void Start()
43-
{
44-
Physics.IgnoreCollision( shaftRB.GetComponent<Collider>(), Player.instance.headCollider );
47+
48+
private void Awake()
49+
{
50+
initialMass = shaftRB.mass;
51+
initialDrag = shaftRB.drag;
52+
initialAngularDrag = shaftRB.angularDrag;
53+
initialInterpolation = shaftRB.interpolation;
54+
initialCollisionDetection = shaftRB.collisionDetectionMode;
55+
initialUseGravity = shaftRB.useGravity;
56+
Destroy(this.GetComponent<Rigidbody>());
57+
}
58+
59+
//-------------------------------------------------
60+
void Start()
61+
{
62+
Physics.IgnoreCollision(this.GetComponent<Collider>(), Player.instance.headCollider);
4563
}
4664

4765

@@ -52,17 +70,35 @@ void FixedUpdate()
5270
{
5371
prevPosition = transform.position;
5472
prevRotation = transform.rotation;
55-
prevVelocity = GetComponent<Rigidbody>().velocity;
73+
prevVelocity = shaftRB.velocity;
5674
prevHeadPosition = arrowHeadRB.transform.position;
5775
travelledFrames++;
5876
}
5977
}
6078

6179

80+
public void StartRelease()
81+
{
82+
Rigidbody rb = this.gameObject.AddComponent<Rigidbody>();
83+
rb.isKinematic = true;
84+
if (shaftRB == null)
85+
shaftRB = rb;
86+
87+
shaftRB.mass = initialMass;
88+
shaftRB.drag = initialDrag;
89+
shaftRB.angularDrag = initialAngularDrag;
90+
shaftRB.interpolation = initialInterpolation;
91+
shaftRB.collisionDetectionMode = initialCollisionDetection;
92+
shaftRB.useGravity = initialUseGravity;
93+
94+
arrowHeadRB.GetComponent<FixedJoint>().connectedBody = rb;
95+
}
96+
97+
6298
//-------------------------------------------------
6399
public void ArrowReleased( float inputVelocity )
64-
{
65-
inFlight = true;
100+
{
101+
inFlight = true;
66102
released = true;
67103

68104
airReleaseSound.Play();

Assets/SteamVR/InteractionSystem/Longbow/Scripts/ArrowHand.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ private void FireArrow()
229229
currentArrow.transform.parent = null;
230230

231231
Arrow arrow = currentArrow.GetComponent<Arrow>();
232-
arrow.shaftRB.isKinematic = false;
232+
arrow.StartRelease();
233+
arrow.shaftRB.isKinematic = false;
233234
arrow.shaftRB.useGravity = true;
234235
arrow.shaftRB.transform.GetComponent<BoxCollider>().enabled = true;
235236

@@ -240,6 +241,9 @@ private void FireArrow()
240241
arrow.arrowHeadRB.AddForce( currentArrow.transform.forward * bow.GetArrowVelocity(), ForceMode.VelocityChange );
241242
arrow.arrowHeadRB.AddTorque( currentArrow.transform.forward * 10 );
242243

244+
arrow.shaftRB.velocity = arrow.arrowHeadRB.velocity;
245+
arrow.shaftRB.angularVelocity = arrow.arrowHeadRB.angularVelocity;
246+
243247
nocked = false;
244248
nockedWithType = GrabTypes.None;
245249

0 commit comments

Comments
 (0)