Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using TMPro;
- using Photon.Pun;
- using Photon.Realtime;
- public class Player : MonoBehaviourPunCallbacks
- {
- [Header("Texts")]
- public TMP_Text usernameText;
- [Header("Photon View")]
- public PhotonView view;
- [Header("Movement")]
- public Rigidbody rb;
- public float moveSpeed = 6f;
- public float moveMultiplier = 10f;
- public float airMultiplier = 0.4f;
- public float jumpForce = 40f;
- [Header("Ground Detection")]
- public Transform groundCheck;
- [Header("Keycodes")]
- public KeyCode jumpKey = KeyCode.Tab;
- [Header("Bools")]
- public bool isGrounded;
- [Header("Colliders")]
- public BoxCollider playerCollider;
- [Header("Audio")]
- public bool playAudio = true;
- public AudioSource walkingSound;
- [Header("Script References")]
- public GameOver _gameOver;
- //public Transform currentPlayerTransform;
- float moveH;
- float moveV;
- float playerHight = 2f;
- float rbDrag = 6f;
- float groundDrag = 6f;
- float airDrag = 1f;
- Vector3 moveDir;
- //Rigidbody rb;
- void Awake()
- {
- if (view.IsMine)
- {
- usernameText.text = PhotonNetwork.NickName;
- }
- else
- {
- usernameText.text = view.Owner.NickName;
- usernameText.color = Color.red;
- }
- rb.freezeRotation = true;
- playerCollider.isTrigger = true;
- rb.useGravity = false;
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- Application.targetFrameRate = 200;
- }
- void Update()
- {
- MyInput();
- ControlDrag();
- isGrounded = Physics.Raycast(transform.position, Vector3.down, playerHight / 2 + 0.1f);
- if (Input.GetKeyDown(jumpKey) && isGrounded)
- {
- if (view.IsMine)
- {
- Jump();
- }
- }
- if (Input.GetKeyDown(KeyCode.Escape))
- {
- Cursor.lockState = CursorLockMode.None;
- Cursor.visible = true;
- }
- if(Cursor.visible && Input.GetMouseButtonDown(0))
- {
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- }
- PlayAudio();
- }
- void PlayAudio()
- {
- if (playAudio)
- {
- PlayWalkAudio();
- }
- }
- void PlayWalkAudio()
- {
- if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
- {
- walkingSound.Play();
- }
- else if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.UpArrow))
- {
- walkingSound.Stop();
- }
- else if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) && !isGrounded)
- {
- walkingSound.Stop();
- }
- if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
- {
- walkingSound.Play();
- }
- else if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.LeftArrow))
- {
- walkingSound.Stop();
- }
- else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow) && !isGrounded)
- {
- walkingSound.Stop();
- }
- if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
- {
- walkingSound.Play();
- }
- else if (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.RightArrow))
- {
- walkingSound.Stop();
- }
- else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow) && !isGrounded)
- {
- walkingSound.Stop();
- }
- if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
- {
- walkingSound.Play();
- }
- else if (Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.DownArrow))
- {
- walkingSound.Stop();
- }
- else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow) && !isGrounded)
- {
- walkingSound.Stop();
- }
- }
- void FixedUpdate()
- {
- MovePlayer();
- }
- void MovePlayer()
- {
- if (view.IsMine)
- {
- if (isGrounded)
- {
- print("GROUND");
- rb.AddForce(moveDir.normalized * moveSpeed * moveMultiplier, ForceMode.Acceleration);
- }
- else
- {
- print("AIR");
- rb.useGravity = true;
- playerCollider.isTrigger = false;
- rb.AddForce(moveDir.normalized * moveSpeed * moveMultiplier * airMultiplier, ForceMode.Acceleration);
- }
- }
- }
- void Jump()
- {
- rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
- }
- void ControlDrag()
- {
- if (isGrounded)
- {
- rb.drag = groundDrag;
- }
- else
- {
- rb.drag = airDrag;
- }
- }
- void MyInput()
- {
- if (view.IsMine)
- {
- moveH = Input.GetAxisRaw("Horizontal");
- moveV = Input.GetAxisRaw("Vertical");
- moveDir = transform.forward * moveV + transform.right * moveH;
- }
- }
- private void OnTriggerEnter(Collider other)
- {
- if (other.CompareTag("Shell"))
- {
- print("collided with tag Shell");
- //Debug.Log("Game Over!");
- Destroy(other.gameObject);
- if (view.IsMine)
- {
- _gameOver.gameObject.SetActive(true);
- }
- }
- }
- public void Disconnect()
- {
- PhotonNetwork.LeaveRoom();
- PhotonNetwork.LoadLevel("Lobby");
- }
- public override void OnDisconnected(DisconnectCause cause)
- {
- Debug.Log("DISCONNECTED!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement