BowControlMecanim.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityStandardAssets.Characters.FirstPerson;
  6. public class BowControlMecanim : WeaponsBaseMecanim {
  7. #region Fields
  8. public GameObject arrow = null;
  9. public Transform mountPoint = null;
  10. public GameObject attachedArrow = null;
  11. // basic stats
  12. public int range = 300;
  13. public float damage = 20.0f;
  14. public float maxPenetration = 3.0f;
  15. public float fireRate = 0.5f;
  16. public int impactForce = 50;
  17. public float arrowSpeed = 60.0f;
  18. [HideInInspector] public float baseSpread = 1.0f;
  19. float maxSpread = 4.0f;
  20. public float defaultSpread = 1f;
  21. public float defaultMaxSpread = 4f;
  22. protected bool canAim;
  23. public bool useSmooth = true;
  24. [Range (1f, 10f)]
  25. public float aimSmooth;
  26. [Range(0, 179)]
  27. public float aimFov = 35;
  28. float defaultFov;
  29. float currentFov;
  30. Quaternion camPosition;
  31. Quaternion defaultCamRot;
  32. // bool firePressed = false;
  33. bool isLoaded = false;
  34. #endregion
  35. void Start ()
  36. {
  37. defaultCamRot = Camera.main.transform.localRotation;
  38. defaultFov = Camera.main.fieldOfView;
  39. }
  40. protected override void OnEnable ()
  41. {
  42. base.OnEnable ();
  43. canAim = true;
  44. wpType = WeaponType.Bow;
  45. }
  46. protected override void OnDisable ()
  47. {
  48. base.OnDisable ();
  49. canAim = false;
  50. }
  51. protected override void InputUpdate()
  52. {
  53. if (Input.GetMouseButton (1) && CanAim)
  54. {
  55. if (!isAimed)
  56. isAimed = true;
  57. if (!isFiring)
  58. EmptySlot (false);
  59. if (Input.GetMouseButtonDown (0) && CanFire && isLoaded)
  60. {
  61. isFiring = true;
  62. Fire ();
  63. }
  64. }
  65. else
  66. {
  67. isAimed = false;
  68. isLoaded = false;
  69. }
  70. }
  71. /// <summary>
  72. /// Called by Player Sync, in the end of the IdleToADS animation.
  73. /// </summary>
  74. public void OnFinishArrowLoad ()
  75. {
  76. isLoaded = true;
  77. }
  78. /// <summary>
  79. /// Called by Player Sync, in the end of the Fire animation.
  80. /// </summary>
  81. public void OnArrowShot ()
  82. {
  83. isFiring = false;
  84. }
  85. protected override void UpdateAnimatorParameters ()
  86. {
  87. foreach (Animator a in myAnims)
  88. {
  89. a.SetBool ("firePressed", isFiring);
  90. a.SetBool ("isAimed", isAimed);
  91. }
  92. if (Input.GetKeyDown (KeyCode.R) && playerSync.Anim.GetCurrentAnimatorStateInfo (0).IsName ("Idle"))
  93. {
  94. foreach (Animator a in myAnims)
  95. {
  96. a.SetTrigger ("reload");
  97. }
  98. }
  99. }
  100. void Fire()
  101. {
  102. StartCoroutine (FireOneShot());
  103. SetFireTrigger ();
  104. EmptySlot (true);
  105. isLoaded = false;
  106. Source.clip = FireSound;
  107. Source.spread = Random.Range (1.0f, 1.5f);
  108. Source.pitch = Random.Range (1.0f, 1.05f);
  109. Source.Play();
  110. // PlayWeaponAnimation (weaponAnimations.Weapon_ADS_Fire.name, WrapMode.Once);
  111. // yield return StartCoroutine (WaitAnimationFinish (weaponAnimations.Weapon_ADS_Fire));
  112. // firePressed = false;
  113. // yield return null;
  114. }
  115. void SetFireTrigger ()
  116. {
  117. // SendMessageUpwards ("FireTriggered");
  118. playerSync.FireTriggered ();
  119. foreach (Animator a in myAnims)
  120. {
  121. a.SetTrigger ("fireTrigger");
  122. }
  123. }
  124. IEnumerator FireOneShot()
  125. {
  126. Vector3 position = mountPoint.position;
  127. // set the gun's info into an array to send to the bullet
  128. MissileInfo info = new MissileInfo();
  129. info.damage = damage;
  130. info.impactForce = impactForce;
  131. info.maxPenetration = maxPenetration;
  132. info.maxspread = maxSpread;
  133. info.speed = arrowSpeed;
  134. info.position = this.transform.root.position;
  135. info.lifeTime = range;
  136. Quaternion q = Quaternion.Euler (new Vector3 (0, transform.eulerAngles.y - 90f, -transform.eulerAngles.x));
  137. GameObject newArrow = Instantiate (arrow, position, q) as GameObject;
  138. newArrow.GetComponent<Missile>().SetUp(info);
  139. // newArrow.transform.RotateAround (newArrow.transform.position, newArrow.transform.right, 90f);
  140. newArrow.transform.RotateAround (newArrow.transform.position, newArrow.transform.forward, 90f);
  141. Source.clip = FireSound;
  142. Source.spread = Random.Range (1.0f, 1.5f);
  143. Source.Play();
  144. yield return null;
  145. }
  146. void EmptySlot (bool value)
  147. {
  148. if (value)
  149. attachedArrow.SetActive (false);
  150. else
  151. attachedArrow.SetActive (true);
  152. }
  153. protected override void Aim ()
  154. {
  155. if (isAimed)
  156. {
  157. currentFov = aimFov;
  158. baseSpread = defaultSpread / 2f;
  159. maxSpread = defaultMaxSpread / 2f;
  160. // SendMessageUpwards ("UpdateAimedPosition", true);
  161. // BroadcastMessage ("UpdateSightStatus", true, SendMessageOptions.DontRequireReceiver);
  162. }
  163. else
  164. {
  165. currentFov = defaultFov;
  166. baseSpread = defaultSpread;
  167. maxSpread = defaultMaxSpread;
  168. // SendMessageUpwards ("UpdateAimedPosition", false);
  169. // BroadcastMessage ("UpdateSightStatus", false, SendMessageOptions.DontRequireReceiver);
  170. }
  171. Camera.main.fieldOfView = useSmooth ?
  172. Mathf.Lerp(Camera.main.fieldOfView, currentFov, Time.deltaTime * (aimSmooth * 3)) : //apply fog distance
  173. Mathf.Lerp(Camera.main.fieldOfView, currentFov, Time.deltaTime * aimSmooth);
  174. }
  175. public bool CanAim
  176. {
  177. get
  178. {
  179. if (canAim && !controller.IsRunning )
  180. return true;
  181. else
  182. return false;
  183. }
  184. }
  185. }