Arrow.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: The arrow for the longbow
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. namespace Valve.VR.InteractionSystem
  9. {
  10. //-------------------------------------------------------------------------
  11. public class Arrow : MonoBehaviour
  12. {
  13. public ParticleSystem glintParticle;
  14. public Rigidbody arrowHeadRB;
  15. public Rigidbody shaftRB;
  16. public PhysicMaterial targetPhysMaterial;
  17. private Vector3 prevPosition;
  18. private Quaternion prevRotation;
  19. private Vector3 prevVelocity;
  20. private Vector3 prevHeadPosition;
  21. public SoundPlayOneshot fireReleaseSound;
  22. public SoundPlayOneshot airReleaseSound;
  23. public SoundPlayOneshot hitTargetSound;
  24. public PlaySound hitGroundSound;
  25. private bool inFlight;
  26. private bool released;
  27. private bool hasSpreadFire = false;
  28. private int travelledFrames = 0;
  29. private GameObject scaleParentObject = null;
  30. //-------------------------------------------------
  31. void Start()
  32. {
  33. Physics.IgnoreCollision( shaftRB.GetComponent<Collider>(), Player.instance.headCollider );
  34. }
  35. //-------------------------------------------------
  36. void FixedUpdate()
  37. {
  38. if ( released && inFlight )
  39. {
  40. prevPosition = transform.position;
  41. prevRotation = transform.rotation;
  42. prevVelocity = GetComponent<Rigidbody>().velocity;
  43. prevHeadPosition = arrowHeadRB.transform.position;
  44. travelledFrames++;
  45. }
  46. }
  47. //-------------------------------------------------
  48. public void ArrowReleased( float inputVelocity )
  49. {
  50. inFlight = true;
  51. released = true;
  52. airReleaseSound.Play();
  53. if ( glintParticle != null )
  54. {
  55. glintParticle.Play();
  56. }
  57. if ( gameObject.GetComponentInChildren<FireSource>().isBurning )
  58. {
  59. fireReleaseSound.Play();
  60. }
  61. // Check if arrow is shot inside or too close to an object
  62. RaycastHit[] hits = Physics.SphereCastAll( transform.position, 0.01f, transform.forward, 0.80f, Physics.DefaultRaycastLayers, QueryTriggerInteraction.Ignore );
  63. foreach ( RaycastHit hit in hits )
  64. {
  65. if ( hit.collider.gameObject != gameObject && hit.collider.gameObject != arrowHeadRB.gameObject && hit.collider != Player.instance.headCollider )
  66. {
  67. Destroy( gameObject );
  68. return;
  69. }
  70. }
  71. travelledFrames = 0;
  72. prevPosition = transform.position;
  73. prevRotation = transform.rotation;
  74. prevHeadPosition = arrowHeadRB.transform.position;
  75. prevVelocity = GetComponent<Rigidbody>().velocity;
  76. Destroy( gameObject, 30 );
  77. }
  78. //-------------------------------------------------
  79. void OnCollisionEnter( Collision collision )
  80. {
  81. if ( inFlight )
  82. {
  83. Rigidbody rb = GetComponent<Rigidbody>();
  84. float rbSpeed = rb.velocity.sqrMagnitude;
  85. bool canStick = ( targetPhysMaterial != null && collision.collider.sharedMaterial == targetPhysMaterial && rbSpeed > 0.2f );
  86. bool hitBalloon = collision.collider.gameObject.GetComponent<Balloon>() != null;
  87. if ( travelledFrames < 2 && !canStick )
  88. {
  89. // Reset transform but halve your velocity
  90. transform.position = prevPosition - prevVelocity * Time.deltaTime;
  91. transform.rotation = prevRotation;
  92. Vector3 reflfectDir = Vector3.Reflect( arrowHeadRB.velocity, collision.contacts[0].normal );
  93. arrowHeadRB.velocity = reflfectDir * 0.25f;
  94. shaftRB.velocity = reflfectDir * 0.25f;
  95. travelledFrames = 0;
  96. return;
  97. }
  98. if ( glintParticle != null )
  99. {
  100. glintParticle.Stop( true );
  101. }
  102. // Only play hit sounds if we're moving quickly
  103. if ( rbSpeed > 0.1f )
  104. {
  105. hitGroundSound.Play();
  106. }
  107. FireSource arrowFire = gameObject.GetComponentInChildren<FireSource>();
  108. FireSource fireSourceOnTarget = collision.collider.GetComponentInParent<FireSource>();
  109. if ( arrowFire != null && arrowFire.isBurning && ( fireSourceOnTarget != null ) )
  110. {
  111. if ( !hasSpreadFire )
  112. {
  113. collision.collider.gameObject.SendMessageUpwards( "FireExposure", gameObject, SendMessageOptions.DontRequireReceiver );
  114. hasSpreadFire = true;
  115. }
  116. }
  117. else
  118. {
  119. // Only count collisions with good speed so that arrows on the ground can't deal damage
  120. // always pop balloons
  121. if ( rbSpeed > 0.1f || hitBalloon )
  122. {
  123. collision.collider.gameObject.SendMessageUpwards( "ApplyDamage", SendMessageOptions.DontRequireReceiver );
  124. gameObject.SendMessage( "HasAppliedDamage", SendMessageOptions.DontRequireReceiver );
  125. }
  126. }
  127. if ( hitBalloon )
  128. {
  129. // Revert my physics properties cause I don't want balloons to influence my travel
  130. transform.position = prevPosition;
  131. transform.rotation = prevRotation;
  132. arrowHeadRB.velocity = prevVelocity;
  133. Physics.IgnoreCollision( arrowHeadRB.GetComponent<Collider>(), collision.collider );
  134. Physics.IgnoreCollision( shaftRB.GetComponent<Collider>(), collision.collider );
  135. }
  136. if ( canStick )
  137. {
  138. StickInTarget( collision, travelledFrames < 2 );
  139. }
  140. // Player Collision Check (self hit)
  141. if ( Player.instance && collision.collider == Player.instance.headCollider )
  142. {
  143. Player.instance.PlayerShotSelf();
  144. }
  145. }
  146. }
  147. //-------------------------------------------------
  148. private void StickInTarget( Collision collision, bool bSkipRayCast )
  149. {
  150. Vector3 prevForward = prevRotation * Vector3.forward;
  151. // Only stick in target if the collider is front of the arrow head
  152. if ( !bSkipRayCast )
  153. {
  154. RaycastHit[] hitInfo;
  155. hitInfo = Physics.RaycastAll( prevHeadPosition - prevVelocity * Time.deltaTime, prevForward, prevVelocity.magnitude * Time.deltaTime * 2.0f );
  156. bool properHit = false;
  157. for ( int i = 0; i < hitInfo.Length; ++i )
  158. {
  159. RaycastHit hit = hitInfo[i];
  160. if ( hit.collider == collision.collider )
  161. {
  162. properHit = true;
  163. break;
  164. }
  165. }
  166. if ( !properHit )
  167. {
  168. return;
  169. }
  170. }
  171. Destroy( glintParticle );
  172. inFlight = false;
  173. shaftRB.velocity = Vector3.zero;
  174. shaftRB.angularVelocity = Vector3.zero;
  175. shaftRB.isKinematic = true;
  176. shaftRB.useGravity = false;
  177. shaftRB.transform.GetComponent<BoxCollider>().enabled = false;
  178. arrowHeadRB.velocity = Vector3.zero;
  179. arrowHeadRB.angularVelocity = Vector3.zero;
  180. arrowHeadRB.isKinematic = true;
  181. arrowHeadRB.useGravity = false;
  182. arrowHeadRB.transform.GetComponent<BoxCollider>().enabled = false;
  183. hitTargetSound.Play();
  184. // If the hit item has a parent, dock an empty object to that
  185. // this fixes an issue with scaling hierarchy. I suspect this is not sustainable for a large object / scaling hierarchy.
  186. scaleParentObject = new GameObject( "Arrow Scale Parent" );
  187. Transform parentTransform = collision.collider.transform;
  188. // Don't do this for weebles because of how it has a fixed joint
  189. ExplosionWobble wobble = collision.collider.gameObject.GetComponent<ExplosionWobble>();
  190. if ( !wobble )
  191. {
  192. if ( parentTransform.parent )
  193. {
  194. parentTransform = parentTransform.parent;
  195. }
  196. }
  197. scaleParentObject.transform.parent = parentTransform;
  198. // Move the arrow to the place on the target collider we were expecting to hit prior to the impact itself knocking it around
  199. transform.parent = scaleParentObject.transform;
  200. transform.rotation = prevRotation;
  201. transform.position = prevPosition;
  202. transform.position = collision.contacts[0].point - transform.forward * ( 0.75f - ( Util.RemapNumberClamped( prevVelocity.magnitude, 0f, 10f, 0.0f, 0.1f ) + Random.Range( 0.0f, 0.05f ) ) );
  203. }
  204. //-------------------------------------------------
  205. void OnDestroy()
  206. {
  207. if ( scaleParentObject != null )
  208. {
  209. Destroy( scaleParentObject );
  210. }
  211. }
  212. }
  213. }