Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine.Networking;
  4. using UnityEngine.Networking.NetworkSystem;
  5. using UnityEngine;
  6.  
  7. public class SCR_Axe_Throwing : NetworkBehaviour
  8. {
  9.  
  10. //Adapted https://www.youtube.com/watch?v=vduSC2YHFnw
  11. //Adapted https://www.youtube.com/watch?v=RfnTIkfKRFI
  12. //Adapted https://answers.unity.com/questions/696579/getting-a-child-object-out-of-its-parent.html
  13. //Adapted https://docs.unity3d.com/Manual/AnimationParameters.html
  14.  
  15. public GameObject Axe;
  16. [SerializeField] GameObject AxeRec;
  17. [SerializeField] GameObject playerCamera;
  18.  
  19. //Gets the animator
  20. Animator animator;
  21.  
  22. Animator axe_animator;
  23.  
  24. private float AxeThrowingForce = 1000f;
  25.  
  26. [SyncVar]
  27. private bool _HoldingAxe = true;
  28.  
  29. //Returning
  30. private Transform trans;
  31. //Finds out it stuck to a wall.
  32. public static bool Stick = false;
  33.  
  34. [SyncVar]
  35. public NetworkInstanceId parentNetId;
  36.  
  37. //Refering to its start position and where it originates
  38. [SerializeField] GameObject Parent;
  39.  
  40. //If clicked use the update function to bring the axe back
  41. private bool _Click;
  42.  
  43. private bool _ToThrow;
  44.  
  45. void Start()
  46. {
  47.  
  48. if (isLocalPlayer)
  49. {
  50.  
  51. Axe.GetComponent<Rigidbody>().useGravity = false;
  52. Axe.AddComponent<NetworkAnimator>().enabled = true;
  53. animator = GetComponent<Animator>();
  54. axe_animator = Axe.GetComponent<Animator>();
  55. Axe.GetComponent<NetworkAnimator>().animator = axe_animator;
  56. }
  57. }
  58.  
  59. // Update is called once per frame
  60. void Update() {
  61.  
  62. if (!isLocalPlayer)
  63. {
  64. return;
  65. }
  66.  
  67. Throw_Update();
  68.  
  69. if (_ToThrow)
  70. {
  71. animator.SetBool("To_Throw", true);
  72. }
  73. else
  74. {
  75. animator.SetBool("To_Throw", false);
  76. }
  77.  
  78. }
  79.  
  80. void Throw_Update()
  81. {
  82.  
  83. if (_Click && Stick)
  84. {
  85. trans.position = Vector3.Lerp(trans.position, AxeRec.transform.position, Time.deltaTime * 5.5f); //Brings it to me
  86. trans.rotation = Quaternion.Lerp(trans.rotation, AxeRec.transform.rotation, Time.time * 5.5f); //Brings to me in the correct rotation
  87. }
  88.  
  89. if (_HoldingAxe)
  90. {
  91. //Hovers in hand.
  92. Axe.transform.position = AxeRec.transform.position;
  93. Axe.transform.rotation = AxeRec.transform.rotation;
  94.  
  95.  
  96. if (Input.GetMouseButtonDown(0))
  97. {
  98. Axe.GetComponent<BoxCollider>().enabled = true;
  99. Throwing();
  100. }
  101. }
  102.  
  103. if (_HoldingAxe == false)
  104. {
  105. if (Stick)
  106. {
  107. axe_animator.SetBool("_Thrown", false);
  108. trans = Axe.transform; //Trans possition is = to Axe Location
  109.  
  110. if (Input.GetMouseButtonDown(0))
  111. {
  112. _Click = true; //Starts the movement return
  113. StartCoroutine(ReciveThrow()); // Sets it in hand
  114. }
  115. }
  116.  
  117. }
  118. }
  119.  
  120. void Throwing()
  121. {
  122.  
  123. _ToThrow = true;
  124. StartCoroutine(To_Throw()); // Sets it in hand
  125.  
  126. }
  127.  
  128. IEnumerator ReciveThrow()
  129. {
  130.  
  131. //Wait 2 seconds then set it as in hand
  132. yield return new WaitForSeconds(1);
  133. Cmd_ReciveThrow();
  134.  
  135. }
  136.  
  137. IEnumerator To_Throw()
  138. {
  139.  
  140. //Wait 2 seconds then set it as in hand
  141. yield return new WaitForSeconds(1);
  142. Cmd_To_Throw();
  143.  
  144. }
  145.  
  146. [Command]
  147. public void Cmd_To_Throw()
  148. {
  149.  
  150. _ToThrow = false;
  151.  
  152. _HoldingAxe = false;
  153. Axe.GetComponent<Rigidbody>().useGravity = true;
  154. Axe.transform.parent = null;
  155. //Axe.GetComponent<Animation>().enabled = true;
  156. axe_animator.SetBool("_Thrown", true);
  157. Axe.GetComponent<Rigidbody>().AddForce(playerCamera.transform.forward * AxeThrowingForce); //Throws it
  158. Rpc_To_Throw();
  159.  
  160. }
  161.  
  162. [ClientRpc]
  163. public void Rpc_To_Throw()
  164. {
  165.  
  166. _ToThrow = false;
  167.  
  168. _HoldingAxe = false;
  169. Axe.GetComponent<Rigidbody>().useGravity = true;
  170. Axe.transform.parent = null;
  171. //Axe.GetComponent<Animation>().enabled = true;
  172. axe_animator.SetBool("_Thrown", true);
  173. Axe.GetComponent<Rigidbody>().AddForce(playerCamera.transform.forward * AxeThrowingForce); //Throws it
  174.  
  175. }
  176.  
  177.  
  178. [Command]
  179. public void Cmd_ReciveThrow()
  180. {
  181.  
  182.  
  183. Axe.transform.SetParent(Parent.transform); // Makes Parent!
  184.  
  185. Axe.GetComponent<Rigidbody>().useGravity = false; //Turns gravity off
  186. Axe.GetComponent<BoxCollider>().enabled = false; //Box Colliders on
  187.  
  188. Axe.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None; //Removes Contraints
  189. Axe.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation; //Removes Contraints
  190.  
  191. _HoldingAxe = true;
  192. Stick = false;
  193. _Click = false;
  194. RPC_ReciveThrow();
  195.  
  196. }
  197.  
  198. [ClientRpc]
  199. public void RPC_ReciveThrow()
  200. {
  201.  
  202.  
  203. Axe.transform.SetParent(Parent.transform); // Makes Parent!
  204.  
  205. Axe.GetComponent<Rigidbody>().useGravity = false; //Turns gravity off
  206. Axe.GetComponent<BoxCollider>().enabled = false; //Box Colliders on
  207.  
  208. Axe.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None; //Removes Contraints
  209. Axe.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation; //Removes Contraints
  210.  
  211. _HoldingAxe = true;
  212. Stick = false;
  213. _Click = false;
  214.  
  215. }
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement