Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using Mirror;
- using Steamworks;
- using System;
- using QuickStart;
- using UnityEngine.SceneManagement;
- using UnityEngine.Animations.Rigging;
- public class PlayerMovement : NetworkBehaviour
- {
- private SceneScript sceneScript;
- public GameObject PlayerModel;
- public GameObject PlayerRight;
- public GameObject PlayerLeft;
- float playerHeight = 2f;
- public GameObject OrientationGMBJ;
- public GameObject cameraObj;
- [SerializeField] Transform orientation;
- [Header("Health")]
- public int health;
- [Header("Death/Respawn")]
- [Header("Movement")]
- public float moveSpeed = 6f;
- [Header("Sprinting")]
- [SerializeField] float walkSpeed = 4f;
- [SerializeField] float wsprintSpeed = 6f;
- [SerializeField] float acceleration = 10f;
- [Header("Jumping")]
- public float jumpForce = 5f;
- [Header("keyBinds")]
- [SerializeField] KeyCode jumpkey = KeyCode.Space;
- [SerializeField] KeyCode sprintkey = KeyCode.LeftShift;
- public float movementMultiplier = 10f;
- [SerializeField] float airMultiplier = 0.4f;
- float groundDrag = 6f;
- float airDrag = 2f;
- float horizontalMovemnet;
- float verticalMovemt;
- [Header("Ground Detection")]
- [SerializeField] Transform groundCheck;
- [SerializeField] LayerMask groundMask;
- bool isGrounded;
- float groundDistance = 0.4f;
- Vector3 moveDirection;
- Vector3 slopeMoveDirection;
- Rigidbody rb;
- RaycastHit slopeHit;
- //gun stuff
- private int selectedWeaponLocal = 1;
- public GameObject[] weaponArray;
- private Weapon activeWeapon;
- private float weaponCooldownTime;
- [SyncVar(hook = nameof(OnWeaponChanged))]
- public int activeWeaponSynced = 1;
- void OnWeaponChanged(int _Old, int _New)
- {
- // disable old weapon
- // in range and not null
- if (0 < _Old && _Old < weaponArray.Length && weaponArray[_Old] != null)
- weaponArray[_Old].SetActive(false);
- // enable new weapon
- // in range and not null
- if (0 < _New && _New < weaponArray.Length && weaponArray[_New] != null)
- {
- weaponArray[_New].SetActive(true);
- activeWeapon = weaponArray[activeWeaponSynced].GetComponent<Weapon>();
- }
- }
- [Command]
- public void CmdChangeActiveWeapon(int newIndex)
- {
- activeWeaponSynced = newIndex;
- }
- void Awake()
- {
- // disable all weapons
- foreach (var item in weaponArray)
- if (item != null)
- item.SetActive(false);
- if (selectedWeaponLocal < weaponArray.Length && weaponArray[selectedWeaponLocal] != null)
- {
- activeWeapon = weaponArray[selectedWeaponLocal].GetComponent<Weapon>();
- }
- sceneScript = GameObject.FindObjectOfType<SceneScript>();
- }
- public override void OnStartLocalPlayer()
- {
- cameraObj.SetActive(true);
- sceneScript.playerScript = this;
- }
- private bool OnSlope()
- {
- if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight / 2f + 0.5f))
- {
- if (slopeHit.normal != Vector3.up)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- return false;
- }
- private void Start()
- {
- rb = GetComponent<Rigidbody>();
- rb.freezeRotation = true;
- }
- private void Update()
- {
- if (!isLocalPlayer)
- isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
- MyInput();
- ControlDrag();
- ControlSpeed();
- if (Input.GetKeyDown(jumpkey) && isGrounded)
- {
- Jump();
- }
- slopeMoveDirection = Vector3.ProjectOnPlane(moveDirection, slopeHit.normal);
- if (Input.GetButtonDown("Fire2")) //Fire2 is mouse 2nd click and left alt
- {
- selectedWeaponLocal += 1;
- if (selectedWeaponLocal > weaponArray.Length)
- selectedWeaponLocal = 1;
- CmdChangeActiveWeapon(selectedWeaponLocal);
- }
- if (Input.GetButtonDown("Fire1")) //Fire1 is mouse 1st click
- {
- if (activeWeapon && Time.time > weaponCooldownTime && activeWeapon.weaponAmmo > 0)
- {
- weaponCooldownTime = Time.time + activeWeapon.weaponCooldown;
- activeWeapon.weaponAmmo -= 1;
- CmdShootRay();
- }
- }
- }
- [Command]
- void CmdShootRay()
- {
- RpcFireWeapon();
- }
- [ClientRpc]
- void RpcFireWeapon()
- {
- //bulletAudio.Play(); muzzleflash etc
- GameObject bullet = Instantiate(activeWeapon.weaponBullet, activeWeapon.weaponFirePosition.position, activeWeapon.weaponFirePosition.rotation);
- bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * activeWeapon.weaponSpeed;
- Destroy(bullet, activeWeapon.weaponLife);
- }
- void MyInput()
- {
- horizontalMovemnet = Input.GetAxisRaw("Horizontal");
- verticalMovemt = Input.GetAxisRaw("Vertical");
- moveDirection = orientation.forward * verticalMovemt + orientation.right * horizontalMovemnet;
- }
- void FixedUpdate()
- {
- MovePlayer();
- }
- void MovePlayer()
- {
- if (isGrounded && !OnSlope())
- {
- rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
- }
- else if (isGrounded && OnSlope())
- {
- rb.AddForce(slopeMoveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
- }
- else if (!isGrounded)
- {
- rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier * airMultiplier, ForceMode.Acceleration);
- }
- }
- void Jump()
- {
- if (isGrounded)
- {
- rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
- rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
- }
- }
- void ControlSpeed()
- {
- if (Input.GetKey(sprintkey) && isGrounded)
- {
- moveSpeed = Mathf.Lerp(moveSpeed, wsprintSpeed, acceleration * Time.deltaTime);
- }
- else
- {
- moveSpeed = Mathf.Lerp(moveSpeed, walkSpeed, acceleration * Time.deltaTime);
- }
- }
- void ControlDrag()
- {
- if (isGrounded)
- {
- rb.drag = groundDrag;
- }
- else
- {
- rb.drag = airDrag;
- }
- }
- [Command]
- public void CmdSendPlayerMessage()
- {
- if (sceneScript)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment