Advertisement
Guest User

Code

a guest
Mar 29th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.79 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine.UI;
  4. using UnityEngine;
  5. using Photon.Pun;
  6.  
  7. public class Player : MonoBehaviourPun, IPunObservable //1
  8. {
  9. #region Variables
  10.  
  11. public float speed;
  12. public float sprintModifier;
  13. public float JumpForce;
  14. public float SlideModifier;
  15.  
  16. public float CrouchModifier; //1
  17. public float slideAmount; //1
  18. public float crouchAmount; //1
  19. public GameObject standingCollider; //1
  20. public GameObject crouchingcollider; //1
  21.  
  22. public Transform weaponParent;
  23. private Rigidbody rb;
  24. public Camera cam;
  25. public GameObject cameraParent;
  26. public LayerMask ground;
  27. public Transform groundChecker;
  28.  
  29. private Vector3 weaponParentOrigin;
  30.  
  31. private Transform HealthBar;
  32. private Text UiAmmo;
  33.  
  34. private Vector3 CamOrigin;
  35. private float baseFOV;
  36. public float SprintFOVModifier = 1.25f;
  37.  
  38. private float MovementCounter;
  39. private float IdleCounter;
  40. private Vector3 WeaponParentCurrentPosition;
  41.  
  42. public int MaxHealth;
  43. private int CurrentHealth;
  44.  
  45. private bool crouched;
  46.  
  47. public Manager manager;
  48. private Weapon weapon;
  49. private bool Sliding;
  50. private float Slide_Time;
  51. public float LengthOfSlide;
  52. private Vector3 Slide_Dir;
  53.  
  54. private float aimAngle;
  55.  
  56.  
  57. private Vector3 targetWeaponBob;
  58.  
  59. #endregion
  60.  
  61. #region MonoBehaviour Callbacks
  62.  
  63. private void Start()
  64. {
  65. weapon = GetComponent<Weapon>();
  66. manager = GameObject.Find("Manager").GetComponent<Manager>();
  67.  
  68. //Sets health to players Max Health
  69. CurrentHealth = MaxHealth;
  70.  
  71.  
  72. //Sets players that are not you to "Player" layer mask
  73. if (!photonView.IsMine)
  74. {
  75. gameObject.layer = 11;
  76. standingCollider.layer = 11;
  77. crouchingcollider.layer = 11;
  78. }
  79.  
  80. //Checks if you are the owner of the player and sets your camera to active
  81. cameraParent.SetActive(photonView.IsMine);
  82.  
  83. baseFOV = cam.fieldOfView;
  84. CamOrigin = cam.transform.localPosition;
  85.  
  86. if (Camera.main) Camera.main.enabled = false;
  87. rb = GetComponent<Rigidbody>();
  88.  
  89. weaponParentOrigin = weaponParent.localPosition;
  90. WeaponParentCurrentPosition = weaponParentOrigin;
  91.  
  92.  
  93.  
  94. if (photonView.IsMine)
  95. {
  96. HealthBar = GameObject.Find("HUD/Health/Health Bar").transform;
  97. UiAmmo = GameObject.Find("HUD/Ammo/Text").GetComponent<Text>();
  98.  
  99. RefreshHealthBar();
  100. weapon.RefreshAmmo(UiAmmo);
  101. }
  102. }
  103.  
  104.  
  105. private void Update()
  106. {
  107. if (!photonView.IsMine)
  108. {
  109. RefreshMultiplayerState();
  110. return;
  111. }
  112.  
  113. BarrierDeath();
  114. //Inputs
  115. float hMove = Input.GetAxisRaw("Horizontal");
  116. float vMove = Input.GetAxisRaw("Vertical");
  117.  
  118. //Controls
  119. bool sprint = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
  120. bool jump = Input.GetKeyDown(KeyCode.Space);
  121. bool crouch = Input.GetKeyDown(KeyCode.LeftControl);
  122.  
  123. //States
  124. bool isGrounded = Physics.Raycast(groundChecker.position, Vector3.down, 0.15f, ground);
  125. bool isJumping = jump && isGrounded;
  126. bool isSprinting = sprint && vMove > 0 && !isJumping && isGrounded;
  127. bool isCrouching = crouch && !isSprinting && !isJumping && isGrounded;
  128.  
  129.  
  130. //Crouching
  131. if (isCrouching)
  132. {
  133. photonView.RPC("SetCrouch", RpcTarget.All, !crouched);
  134. }
  135.  
  136. //Jumping
  137. if (isJumping)
  138. {
  139. if (crouched) photonView.RPC("SetCrouch", RpcTarget.All, false);
  140. rb.AddForce(Vector3.up * JumpForce);
  141. }
  142.  
  143. if (Input.GetKeyDown(KeyCode.U)) TakeDamage(20);
  144.  
  145. //Headbob
  146.  
  147. if (Sliding)
  148. {
  149. weaponParent.localPosition = Vector3.Lerp(weaponParent.localPosition, targetWeaponBob, Time.deltaTime * 10);
  150. }
  151.  
  152. else if(hMove == 0 && vMove == 0)
  153. {
  154. HeadBob(IdleCounter, 0.025f, 0.025f);
  155. IdleCounter += Time.deltaTime;
  156. weaponParent.localPosition = Vector3.Lerp(weaponParent.localPosition, targetWeaponBob, Time.deltaTime * 2f);
  157. }
  158. else if (!isSprinting)
  159. {
  160. HeadBob(MovementCounter, 0.035f, 0.035f);
  161. MovementCounter += Time.deltaTime * 3;
  162. weaponParent.localPosition = Vector3.Lerp(weaponParent.localPosition, targetWeaponBob, Time.deltaTime * 6);
  163. }
  164. else if (crouched)
  165. {
  166. //crouching
  167. HeadBob(MovementCounter, 0.02f, 0.02f);
  168. MovementCounter += Time.deltaTime * 1.75f;
  169. weaponParent.localPosition = Vector3.Lerp(weaponParent.localPosition, targetWeaponBob, Time.deltaTime * 6);
  170. }
  171. else
  172. {
  173. HeadBob(MovementCounter, 0.1f, 0.05f);
  174. MovementCounter += Time.deltaTime * 5;
  175. weaponParent.localPosition = Vector3.Lerp(weaponParent.localPosition, targetWeaponBob, Time.deltaTime * 10);
  176. }
  177.  
  178. //UIo Refreshes
  179. RefreshHealthBar();
  180. weapon.RefreshAmmo(UiAmmo);
  181. }
  182.  
  183.  
  184. private void FixedUpdate()
  185. {
  186. if (!photonView.IsMine) return;
  187.  
  188. //Inputs
  189. float hMove = Input.GetAxisRaw("Horizontal");
  190. float vMove = Input.GetAxisRaw("Vertical");
  191.  
  192. //Controls
  193. bool sprint = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
  194. bool jump = Input.GetKey(KeyCode.Space);
  195. bool slide = Input.GetKey(KeyCode.F);
  196.  
  197.  
  198. //States
  199. bool isGrounded = Physics.Raycast(groundChecker.position, Vector3.down, 0.1f, ground);
  200. bool isJumping = jump && isGrounded;
  201. bool isSprinting = sprint && vMove > 0 && !isJumping && isGrounded;
  202. bool isSliding = isSprinting && slide && !Sliding;
  203.  
  204.  
  205. //Movement
  206. Vector3 Direction = Vector3.zero;
  207. float adjSpeed = speed;
  208.  
  209. if (!Sliding)
  210. {
  211. Direction = new Vector3(hMove, 0f, vMove);
  212. Direction.Normalize();
  213. Direction = transform.TransformDirection(Direction);
  214.  
  215. if (isSprinting)
  216. {
  217. if (crouched) photonView.RPC("SetCrouch", RpcTarget.All, false);
  218. adjSpeed *= sprintModifier; //issue may be in *=
  219. }
  220. else if (crouched)
  221. {
  222. adjSpeed *= CrouchModifier; //issue may be in *=
  223. }
  224. }
  225. else
  226. {
  227. Direction = Slide_Dir;
  228. adjSpeed *= SlideModifier;
  229. Slide_Time -= Time.deltaTime;
  230. if (Slide_Time <= 0)
  231. {
  232. Sliding = false;
  233. WeaponParentCurrentPosition -= Vector3.down * (slideAmount - crouchAmount);
  234. }
  235. }
  236.  
  237. Vector3 TargetVelocity = Direction * adjSpeed * Time.deltaTime;
  238. TargetVelocity.y = rb.velocity.y;
  239. rb.velocity = TargetVelocity;
  240.  
  241. //sliding
  242. if (isSliding)
  243. {
  244. Sliding = true;
  245. Slide_Dir = Direction;
  246. Slide_Time = LengthOfSlide;
  247. WeaponParentCurrentPosition += Vector3.down * (slideAmount - crouchAmount);
  248. if (!crouched) photonView.RPC("SetCrouch", RpcTarget.All, true);
  249. }
  250.  
  251.  
  252. //Camera Stuff
  253. if (Sliding)
  254. {
  255. cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, baseFOV * SprintFOVModifier * 1.25f, Time.deltaTime * 8f);
  256. cam.transform.localPosition = Vector3.Lerp(cam.transform.localPosition, CamOrigin + Vector3.down * slideAmount, Time.deltaTime * 6f);
  257. }
  258. else
  259. {
  260. if (isSprinting) cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, baseFOV * SprintFOVModifier, Time.deltaTime * 8f);
  261. else cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, baseFOV, Time.deltaTime * 8f);
  262.  
  263. if (crouched) cam.transform.localPosition = Vector3.Lerp(cam.transform.localPosition, CamOrigin + Vector3.down * crouchAmount, Time.deltaTime * 6f);
  264. else cam.transform.localPosition = Vector3.Lerp(cam.transform.localPosition, CamOrigin, Time.deltaTime * 6f);
  265. }
  266. }
  267.  
  268. #endregion
  269.  
  270. #region Private Methods
  271.  
  272. void RefreshMultiplayerState()
  273. {
  274. float cachEulY = weaponParent.localEulerAngles.y;
  275.  
  276. Quaternion targetRotation = Quaternion.identity * Quaternion.AngleAxis(aimAngle, Vector3.right);
  277. weaponParent.rotation = Quaternion.Slerp(weaponParent.rotation, targetRotation, Time.deltaTime * 8f);
  278.  
  279. Vector3 finalRoation = weaponParent.localEulerAngles;
  280. finalRoation.y = cachEulY;
  281.  
  282. weaponParent.localEulerAngles = finalRoation;
  283. }
  284. void HeadBob(float p_z, float p_x_Intentsity, float p_y_Intensity)
  285. {
  286. float t_aim_adj = 1f;
  287. if (weapon.isAiming) t_aim_adj = 0.1f;
  288. targetWeaponBob = WeaponParentCurrentPosition + new Vector3(Mathf.Cos(p_z) * p_x_Intentsity * t_aim_adj, Mathf.Sin(p_z * 2) * p_y_Intensity * t_aim_adj, 0f);
  289. }
  290.  
  291. void RefreshHealthBar()
  292. {
  293. float t_health_ratio = (float)CurrentHealth / (float)MaxHealth;
  294. HealthBar.localScale = Vector3.Lerp(HealthBar.localScale, new Vector3(t_health_ratio, 1f, 1f), Time.deltaTime * 8f);
  295.  
  296. }
  297. void BarrierDeath()
  298. {
  299. if(transform.position.y <= -100)
  300. {
  301. TakeDamage(MaxHealth);
  302. }
  303. }
  304. #endregion
  305.  
  306. #region Public Method
  307.  
  308. public void TakeDamage (int p_damage)
  309. {
  310. if (photonView.IsMine)
  311. {
  312. CurrentHealth -= p_damage;
  313. RefreshHealthBar();
  314.  
  315. if (CurrentHealth <= 0)
  316. {
  317. manager.Spawn();
  318. PhotonNetwork.Destroy(gameObject);
  319. }
  320. }
  321. }
  322.  
  323.  
  324. [PunRPC]
  325. void SetCrouch(bool p_state)
  326. {
  327. if (crouched = p_state) return; //error may be in =
  328.  
  329. crouched = p_state;
  330.  
  331. if (crouched)
  332. {
  333. standingCollider.SetActive(false);
  334. crouchingcollider.SetActive(true);
  335. WeaponParentCurrentPosition += Vector3.down * crouchAmount;
  336. }
  337.  
  338. else
  339. {
  340. standingCollider.SetActive(true);
  341. crouchingcollider.SetActive(false);
  342. WeaponParentCurrentPosition -= Vector3.down * crouchAmount;
  343. }
  344. }
  345.  
  346. public void OnPhotonSerializeView(PhotonStream p_Stream, PhotonMessageInfo p_message)
  347. {
  348. if (p_Stream.IsWriting)
  349. {
  350. p_Stream.SendNext((int)(weaponParent.transform.localEulerAngles.x * 100f));
  351. }
  352. else
  353. {
  354. aimAngle = (int)p_Stream.ReceiveNext() / 100;
  355. }
  356. }
  357. #endregion
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement