Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine.UI;
- using UnityEngine;
- using Photon.Pun;
- public class Player : MonoBehaviourPun, IPunObservable //1
- {
- #region Variables
- public float speed;
- public float sprintModifier;
- public float JumpForce;
- public float SlideModifier;
- public float CrouchModifier; //1
- public float slideAmount; //1
- public float crouchAmount; //1
- public GameObject standingCollider; //1
- public GameObject crouchingcollider; //1
- public Transform weaponParent;
- private Rigidbody rb;
- public Camera cam;
- public GameObject cameraParent;
- public LayerMask ground;
- public Transform groundChecker;
- private Vector3 weaponParentOrigin;
- private Transform HealthBar;
- private Text UiAmmo;
- private Vector3 CamOrigin;
- private float baseFOV;
- public float SprintFOVModifier = 1.25f;
- private float MovementCounter;
- private float IdleCounter;
- private Vector3 WeaponParentCurrentPosition;
- public int MaxHealth;
- private int CurrentHealth;
- private bool crouched;
- public Manager manager;
- private Weapon weapon;
- private bool Sliding;
- private float Slide_Time;
- public float LengthOfSlide;
- private Vector3 Slide_Dir;
- private float aimAngle;
- private Vector3 targetWeaponBob;
- #endregion
- #region MonoBehaviour Callbacks
- private void Start()
- {
- weapon = GetComponent<Weapon>();
- manager = GameObject.Find("Manager").GetComponent<Manager>();
- //Sets health to players Max Health
- CurrentHealth = MaxHealth;
- //Sets players that are not you to "Player" layer mask
- if (!photonView.IsMine)
- {
- gameObject.layer = 11;
- standingCollider.layer = 11;
- crouchingcollider.layer = 11;
- }
- //Checks if you are the owner of the player and sets your camera to active
- cameraParent.SetActive(photonView.IsMine);
- baseFOV = cam.fieldOfView;
- CamOrigin = cam.transform.localPosition;
- if (Camera.main) Camera.main.enabled = false;
- rb = GetComponent<Rigidbody>();
- weaponParentOrigin = weaponParent.localPosition;
- WeaponParentCurrentPosition = weaponParentOrigin;
- if (photonView.IsMine)
- {
- HealthBar = GameObject.Find("HUD/Health/Health Bar").transform;
- UiAmmo = GameObject.Find("HUD/Ammo/Text").GetComponent<Text>();
- RefreshHealthBar();
- weapon.RefreshAmmo(UiAmmo);
- }
- }
- private void Update()
- {
- if (!photonView.IsMine)
- {
- RefreshMultiplayerState();
- return;
- }
- BarrierDeath();
- //Inputs
- float hMove = Input.GetAxisRaw("Horizontal");
- float vMove = Input.GetAxisRaw("Vertical");
- //Controls
- bool sprint = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
- bool jump = Input.GetKeyDown(KeyCode.Space);
- bool crouch = Input.GetKeyDown(KeyCode.LeftControl);
- //States
- bool isGrounded = Physics.Raycast(groundChecker.position, Vector3.down, 0.15f, ground);
- bool isJumping = jump && isGrounded;
- bool isSprinting = sprint && vMove > 0 && !isJumping && isGrounded;
- bool isCrouching = crouch && !isSprinting && !isJumping && isGrounded;
- //Crouching
- if (isCrouching)
- {
- photonView.RPC("SetCrouch", RpcTarget.All, !crouched);
- }
- //Jumping
- if (isJumping)
- {
- if (crouched) photonView.RPC("SetCrouch", RpcTarget.All, false);
- rb.AddForce(Vector3.up * JumpForce);
- }
- if (Input.GetKeyDown(KeyCode.U)) TakeDamage(20);
- //Headbob
- if (Sliding)
- {
- weaponParent.localPosition = Vector3.Lerp(weaponParent.localPosition, targetWeaponBob, Time.deltaTime * 10);
- }
- else if(hMove == 0 && vMove == 0)
- {
- HeadBob(IdleCounter, 0.025f, 0.025f);
- IdleCounter += Time.deltaTime;
- weaponParent.localPosition = Vector3.Lerp(weaponParent.localPosition, targetWeaponBob, Time.deltaTime * 2f);
- }
- else if (!isSprinting)
- {
- HeadBob(MovementCounter, 0.035f, 0.035f);
- MovementCounter += Time.deltaTime * 3;
- weaponParent.localPosition = Vector3.Lerp(weaponParent.localPosition, targetWeaponBob, Time.deltaTime * 6);
- }
- else if (crouched)
- {
- //crouching
- HeadBob(MovementCounter, 0.02f, 0.02f);
- MovementCounter += Time.deltaTime * 1.75f;
- weaponParent.localPosition = Vector3.Lerp(weaponParent.localPosition, targetWeaponBob, Time.deltaTime * 6);
- }
- else
- {
- HeadBob(MovementCounter, 0.1f, 0.05f);
- MovementCounter += Time.deltaTime * 5;
- weaponParent.localPosition = Vector3.Lerp(weaponParent.localPosition, targetWeaponBob, Time.deltaTime * 10);
- }
- //UIo Refreshes
- RefreshHealthBar();
- weapon.RefreshAmmo(UiAmmo);
- }
- private void FixedUpdate()
- {
- if (!photonView.IsMine) return;
- //Inputs
- float hMove = Input.GetAxisRaw("Horizontal");
- float vMove = Input.GetAxisRaw("Vertical");
- //Controls
- bool sprint = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
- bool jump = Input.GetKey(KeyCode.Space);
- bool slide = Input.GetKey(KeyCode.F);
- //States
- bool isGrounded = Physics.Raycast(groundChecker.position, Vector3.down, 0.1f, ground);
- bool isJumping = jump && isGrounded;
- bool isSprinting = sprint && vMove > 0 && !isJumping && isGrounded;
- bool isSliding = isSprinting && slide && !Sliding;
- //Movement
- Vector3 Direction = Vector3.zero;
- float adjSpeed = speed;
- if (!Sliding)
- {
- Direction = new Vector3(hMove, 0f, vMove);
- Direction.Normalize();
- Direction = transform.TransformDirection(Direction);
- if (isSprinting)
- {
- if (crouched) photonView.RPC("SetCrouch", RpcTarget.All, false);
- adjSpeed *= sprintModifier; //issue may be in *=
- }
- else if (crouched)
- {
- adjSpeed *= CrouchModifier; //issue may be in *=
- }
- }
- else
- {
- Direction = Slide_Dir;
- adjSpeed *= SlideModifier;
- Slide_Time -= Time.deltaTime;
- if (Slide_Time <= 0)
- {
- Sliding = false;
- WeaponParentCurrentPosition -= Vector3.down * (slideAmount - crouchAmount);
- }
- }
- Vector3 TargetVelocity = Direction * adjSpeed * Time.deltaTime;
- TargetVelocity.y = rb.velocity.y;
- rb.velocity = TargetVelocity;
- //sliding
- if (isSliding)
- {
- Sliding = true;
- Slide_Dir = Direction;
- Slide_Time = LengthOfSlide;
- WeaponParentCurrentPosition += Vector3.down * (slideAmount - crouchAmount);
- if (!crouched) photonView.RPC("SetCrouch", RpcTarget.All, true);
- }
- //Camera Stuff
- if (Sliding)
- {
- cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, baseFOV * SprintFOVModifier * 1.25f, Time.deltaTime * 8f);
- cam.transform.localPosition = Vector3.Lerp(cam.transform.localPosition, CamOrigin + Vector3.down * slideAmount, Time.deltaTime * 6f);
- }
- else
- {
- if (isSprinting) cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, baseFOV * SprintFOVModifier, Time.deltaTime * 8f);
- else cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, baseFOV, Time.deltaTime * 8f);
- if (crouched) cam.transform.localPosition = Vector3.Lerp(cam.transform.localPosition, CamOrigin + Vector3.down * crouchAmount, Time.deltaTime * 6f);
- else cam.transform.localPosition = Vector3.Lerp(cam.transform.localPosition, CamOrigin, Time.deltaTime * 6f);
- }
- }
- #endregion
- #region Private Methods
- void RefreshMultiplayerState()
- {
- float cachEulY = weaponParent.localEulerAngles.y;
- Quaternion targetRotation = Quaternion.identity * Quaternion.AngleAxis(aimAngle, Vector3.right);
- weaponParent.rotation = Quaternion.Slerp(weaponParent.rotation, targetRotation, Time.deltaTime * 8f);
- Vector3 finalRoation = weaponParent.localEulerAngles;
- finalRoation.y = cachEulY;
- weaponParent.localEulerAngles = finalRoation;
- }
- void HeadBob(float p_z, float p_x_Intentsity, float p_y_Intensity)
- {
- float t_aim_adj = 1f;
- if (weapon.isAiming) t_aim_adj = 0.1f;
- 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);
- }
- void RefreshHealthBar()
- {
- float t_health_ratio = (float)CurrentHealth / (float)MaxHealth;
- HealthBar.localScale = Vector3.Lerp(HealthBar.localScale, new Vector3(t_health_ratio, 1f, 1f), Time.deltaTime * 8f);
- }
- void BarrierDeath()
- {
- if(transform.position.y <= -100)
- {
- TakeDamage(MaxHealth);
- }
- }
- #endregion
- #region Public Method
- public void TakeDamage (int p_damage)
- {
- if (photonView.IsMine)
- {
- CurrentHealth -= p_damage;
- RefreshHealthBar();
- if (CurrentHealth <= 0)
- {
- manager.Spawn();
- PhotonNetwork.Destroy(gameObject);
- }
- }
- }
- [PunRPC]
- void SetCrouch(bool p_state)
- {
- if (crouched = p_state) return; //error may be in =
- crouched = p_state;
- if (crouched)
- {
- standingCollider.SetActive(false);
- crouchingcollider.SetActive(true);
- WeaponParentCurrentPosition += Vector3.down * crouchAmount;
- }
- else
- {
- standingCollider.SetActive(true);
- crouchingcollider.SetActive(false);
- WeaponParentCurrentPosition -= Vector3.down * crouchAmount;
- }
- }
- public void OnPhotonSerializeView(PhotonStream p_Stream, PhotonMessageInfo p_message)
- {
- if (p_Stream.IsWriting)
- {
- p_Stream.SendNext((int)(weaponParent.transform.localEulerAngles.x * 100f));
- }
- else
- {
- aimAngle = (int)p_Stream.ReceiveNext() / 100;
- }
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement