Guest User

Untitled

a guest
Jan 15th, 2024
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Mirror;
  5. using Steamworks;
  6. using System;
  7. using QuickStart;
  8. using UnityEngine.SceneManagement;
  9. using UnityEngine.Animations.Rigging;
  10.  
  11.  
  12. public class PlayerMovement : NetworkBehaviour
  13. {
  14.  
  15.  
  16. private SceneScript sceneScript;
  17. public GameObject PlayerModel;
  18. public GameObject PlayerRight;
  19. public GameObject PlayerLeft;
  20.  
  21.  
  22.  
  23.  
  24. float playerHeight = 2f;
  25.  
  26. public GameObject OrientationGMBJ;
  27. public GameObject cameraObj;
  28.  
  29. [SerializeField] Transform orientation;
  30.  
  31. [Header("Health")]
  32. public int health;
  33.  
  34. [Header("Death/Respawn")]
  35.  
  36.  
  37. [Header("Movement")]
  38. public float moveSpeed = 6f;
  39.  
  40. [Header("Sprinting")]
  41. [SerializeField] float walkSpeed = 4f;
  42. [SerializeField] float wsprintSpeed = 6f;
  43. [SerializeField] float acceleration = 10f;
  44.  
  45. [Header("Jumping")]
  46. public float jumpForce = 5f;
  47.  
  48. [Header("keyBinds")]
  49. [SerializeField] KeyCode jumpkey = KeyCode.Space;
  50. [SerializeField] KeyCode sprintkey = KeyCode.LeftShift;
  51.  
  52. public float movementMultiplier = 10f;
  53. [SerializeField] float airMultiplier = 0.4f;
  54. float groundDrag = 6f;
  55. float airDrag = 2f;
  56.  
  57. float horizontalMovemnet;
  58. float verticalMovemt;
  59.  
  60. [Header("Ground Detection")]
  61. [SerializeField] Transform groundCheck;
  62. [SerializeField] LayerMask groundMask;
  63. bool isGrounded;
  64. float groundDistance = 0.4f;
  65.  
  66.  
  67. Vector3 moveDirection;
  68. Vector3 slopeMoveDirection;
  69.  
  70. Rigidbody rb;
  71.  
  72. RaycastHit slopeHit;
  73.  
  74.  
  75. //gun stuff
  76. private int selectedWeaponLocal = 1;
  77. public GameObject[] weaponArray;
  78. private Weapon activeWeapon;
  79. private float weaponCooldownTime;
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86. [SyncVar(hook = nameof(OnWeaponChanged))]
  87. public int activeWeaponSynced = 1;
  88.  
  89.  
  90.  
  91. void OnWeaponChanged(int _Old, int _New)
  92. {
  93. // disable old weapon
  94. // in range and not null
  95. if (0 < _Old && _Old < weaponArray.Length && weaponArray[_Old] != null)
  96. weaponArray[_Old].SetActive(false);
  97.  
  98.  
  99. // enable new weapon
  100. // in range and not null
  101. if (0 < _New && _New < weaponArray.Length && weaponArray[_New] != null)
  102. {
  103. weaponArray[_New].SetActive(true);
  104. activeWeapon = weaponArray[activeWeaponSynced].GetComponent<Weapon>();
  105.  
  106.  
  107. }
  108. }
  109.  
  110.  
  111.  
  112.  
  113.  
  114. [Command]
  115. public void CmdChangeActiveWeapon(int newIndex)
  116. {
  117.  
  118. activeWeaponSynced = newIndex;
  119. }
  120.  
  121. void Awake()
  122. {
  123.  
  124. // disable all weapons
  125. foreach (var item in weaponArray)
  126. if (item != null)
  127. item.SetActive(false);
  128.  
  129. if (selectedWeaponLocal < weaponArray.Length && weaponArray[selectedWeaponLocal] != null)
  130. {
  131. activeWeapon = weaponArray[selectedWeaponLocal].GetComponent<Weapon>();
  132.  
  133. }
  134.  
  135. sceneScript = GameObject.FindObjectOfType<SceneScript>();
  136. }
  137.  
  138. public override void OnStartLocalPlayer()
  139. {
  140.  
  141. cameraObj.SetActive(true);
  142. sceneScript.playerScript = this;
  143.  
  144.  
  145. }
  146.  
  147.  
  148.  
  149.  
  150. private bool OnSlope()
  151. {
  152.  
  153.  
  154. if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight / 2f + 0.5f))
  155. {
  156. if (slopeHit.normal != Vector3.up)
  157. {
  158. return true;
  159. }
  160. else
  161. {
  162. return false;
  163. }
  164. }
  165. return false;
  166. }
  167.  
  168.  
  169.  
  170. private void Start()
  171. {
  172.  
  173.  
  174.  
  175. rb = GetComponent<Rigidbody>();
  176. rb.freezeRotation = true;
  177.  
  178. }
  179.  
  180.  
  181.  
  182. private void Update()
  183. {
  184.  
  185. if (!isLocalPlayer)
  186.  
  187.  
  188. isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
  189.  
  190.  
  191. MyInput();
  192. ControlDrag();
  193. ControlSpeed();
  194.  
  195.  
  196.  
  197. if (Input.GetKeyDown(jumpkey) && isGrounded)
  198. {
  199. Jump();
  200. }
  201.  
  202. slopeMoveDirection = Vector3.ProjectOnPlane(moveDirection, slopeHit.normal);
  203.  
  204.  
  205. if (Input.GetButtonDown("Fire2")) //Fire2 is mouse 2nd click and left alt
  206. {
  207. selectedWeaponLocal += 1;
  208.  
  209. if (selectedWeaponLocal > weaponArray.Length)
  210. selectedWeaponLocal = 1;
  211.  
  212. CmdChangeActiveWeapon(selectedWeaponLocal);
  213. }
  214.  
  215.  
  216.  
  217.  
  218. if (Input.GetButtonDown("Fire1")) //Fire1 is mouse 1st click
  219. {
  220. if (activeWeapon && Time.time > weaponCooldownTime && activeWeapon.weaponAmmo > 0)
  221. {
  222. weaponCooldownTime = Time.time + activeWeapon.weaponCooldown;
  223. activeWeapon.weaponAmmo -= 1;
  224.  
  225. CmdShootRay();
  226. }
  227. }
  228.  
  229.  
  230.  
  231. }
  232.  
  233. [Command]
  234. void CmdShootRay()
  235. {
  236.  
  237.  
  238.  
  239. RpcFireWeapon();
  240. }
  241.  
  242. [ClientRpc]
  243. void RpcFireWeapon()
  244. {
  245.  
  246.  
  247. //bulletAudio.Play(); muzzleflash etc
  248. GameObject bullet = Instantiate(activeWeapon.weaponBullet, activeWeapon.weaponFirePosition.position, activeWeapon.weaponFirePosition.rotation);
  249. bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * activeWeapon.weaponSpeed;
  250. Destroy(bullet, activeWeapon.weaponLife);
  251. }
  252.  
  253.  
  254.  
  255. void MyInput()
  256. {
  257.  
  258.  
  259. horizontalMovemnet = Input.GetAxisRaw("Horizontal");
  260. verticalMovemt = Input.GetAxisRaw("Vertical");
  261.  
  262. moveDirection = orientation.forward * verticalMovemt + orientation.right * horizontalMovemnet;
  263. }
  264.  
  265.  
  266.  
  267.  
  268. void FixedUpdate()
  269. {
  270. MovePlayer();
  271. }
  272.  
  273.  
  274.  
  275.  
  276. void MovePlayer()
  277. {
  278.  
  279. if (isGrounded && !OnSlope())
  280. {
  281. rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
  282. }
  283. else if (isGrounded && OnSlope())
  284. {
  285. rb.AddForce(slopeMoveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
  286. }
  287. else if (!isGrounded)
  288. {
  289. rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier * airMultiplier, ForceMode.Acceleration);
  290. }
  291. }
  292.  
  293.  
  294. void Jump()
  295. {
  296.  
  297. if (isGrounded)
  298. {
  299. rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
  300. rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
  301. }
  302.  
  303. }
  304.  
  305. void ControlSpeed()
  306. {
  307.  
  308. if (Input.GetKey(sprintkey) && isGrounded)
  309. {
  310. moveSpeed = Mathf.Lerp(moveSpeed, wsprintSpeed, acceleration * Time.deltaTime);
  311. }
  312. else
  313. {
  314. moveSpeed = Mathf.Lerp(moveSpeed, walkSpeed, acceleration * Time.deltaTime);
  315. }
  316. }
  317.  
  318. void ControlDrag()
  319. {
  320.  
  321. if (isGrounded)
  322. {
  323. rb.drag = groundDrag;
  324. }
  325. else
  326. {
  327. rb.drag = airDrag;
  328. }
  329. }
  330.  
  331.  
  332. [Command]
  333. public void CmdSendPlayerMessage()
  334. {
  335. if (sceneScript)
  336. {
  337.  
  338.  
  339. }
  340.  
  341. }
  342.  
  343.  
  344.  
  345.  
  346. }
  347.  
  348.  
Advertisement
Add Comment
Please, Sign In to add comment