Advertisement
Saharsh11

Untitled

Oct 28th, 2021
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.92 KB | None | 0 0
  1. using UnityEngine;
  2. using TMPro;
  3. using Photon.Pun;
  4. using Photon.Realtime;
  5.  
  6. public class Player : MonoBehaviourPunCallbacks
  7. {
  8.     [Header("Texts")]
  9.     public TMP_Text usernameText;
  10.  
  11.     [Header("Photon View")]
  12.     public PhotonView view;
  13.  
  14.     [Header("Movement")]
  15.     public Rigidbody rb;
  16.     public float moveSpeed = 6f;
  17.     public float moveMultiplier = 10f;
  18.     public float airMultiplier = 0.4f;
  19.     public float jumpForce = 40f;
  20.  
  21.     [Header("Ground Detection")]
  22.     public Transform groundCheck;
  23.  
  24.     [Header("Keycodes")]
  25.     public KeyCode jumpKey = KeyCode.Tab;
  26.  
  27.     [Header("Bools")]
  28.     public bool isGrounded;
  29.  
  30.     [Header("Colliders")]
  31.     public BoxCollider playerCollider;
  32.  
  33.     [Header("Audio")]
  34.     public bool playAudio = true;
  35.     public AudioSource walkingSound;
  36.  
  37.     [Header("Script References")]
  38.     public GameOver _gameOver;
  39.  
  40.     //public Transform currentPlayerTransform;
  41.  
  42.     float moveH;
  43.     float moveV;
  44.  
  45.     float playerHight = 2f;
  46.  
  47.     float rbDrag = 6f;
  48.  
  49.     float groundDrag = 6f;
  50.     float airDrag = 1f;
  51.  
  52.     Vector3 moveDir;
  53.  
  54.     //Rigidbody rb;
  55.  
  56.     void Awake()
  57.     {
  58.         if (view.IsMine)
  59.         {
  60.             usernameText.text = PhotonNetwork.NickName;
  61.         }
  62.         else
  63.         {
  64.             usernameText.text = view.Owner.NickName;
  65.             usernameText.color = Color.red;
  66.         }
  67.  
  68.         rb.freezeRotation = true;
  69.         playerCollider.isTrigger = true;
  70.         rb.useGravity = false;
  71.  
  72.         Cursor.lockState = CursorLockMode.Locked;
  73.         Cursor.visible = false;
  74.  
  75.         Application.targetFrameRate = 200;
  76.     }
  77.  
  78.     void Update()
  79.     {
  80.         MyInput();
  81.         ControlDrag();
  82.  
  83.         isGrounded = Physics.Raycast(transform.position, Vector3.down, playerHight / 2 + 0.1f);
  84.  
  85.         if (Input.GetKeyDown(jumpKey) && isGrounded)
  86.         {
  87.             if (view.IsMine)
  88.             {
  89.                 Jump();
  90.             }
  91.         }
  92.  
  93.         if (Input.GetKeyDown(KeyCode.Escape))
  94.         {
  95.             Cursor.lockState = CursorLockMode.None;
  96.             Cursor.visible = true;
  97.         }
  98.  
  99.         if(Cursor.visible && Input.GetMouseButtonDown(0))
  100.         {
  101.             Cursor.lockState = CursorLockMode.Locked;
  102.             Cursor.visible = false;
  103.         }
  104.  
  105.         PlayAudio();
  106.     }
  107.  
  108.     void PlayAudio()
  109.     {
  110.         if (playAudio)
  111.         {
  112.             PlayWalkAudio();
  113.         }
  114.     }
  115.  
  116.     void PlayWalkAudio()
  117.     {
  118.         if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
  119.         {
  120.             walkingSound.Play();
  121.         }
  122.         else if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.UpArrow))
  123.         {
  124.             walkingSound.Stop();
  125.         }
  126.         else if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) && !isGrounded)
  127.         {
  128.             walkingSound.Stop();
  129.         }
  130.  
  131.  
  132.         if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
  133.         {
  134.             walkingSound.Play();
  135.         }
  136.         else if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.LeftArrow))
  137.         {
  138.             walkingSound.Stop();
  139.         }
  140.         else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow) && !isGrounded)
  141.         {
  142.             walkingSound.Stop();
  143.         }
  144.  
  145.  
  146.         if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
  147.         {
  148.             walkingSound.Play();
  149.         }
  150.         else if (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.RightArrow))
  151.         {
  152.             walkingSound.Stop();
  153.         }
  154.         else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow) && !isGrounded)
  155.         {
  156.             walkingSound.Stop();
  157.         }
  158.  
  159.  
  160.         if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
  161.         {
  162.             walkingSound.Play();
  163.         }
  164.         else if (Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.DownArrow))
  165.         {
  166.             walkingSound.Stop();
  167.         }
  168.         else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow) && !isGrounded)
  169.         {
  170.             walkingSound.Stop();
  171.         }
  172.     }
  173.  
  174.     void FixedUpdate()
  175.     {
  176.         MovePlayer();
  177.     }
  178.  
  179.     void MovePlayer()
  180.     {
  181.         if (view.IsMine)
  182.         {
  183.             if (isGrounded)
  184.             {
  185.                 print("GROUND");
  186.                 rb.AddForce(moveDir.normalized * moveSpeed * moveMultiplier, ForceMode.Acceleration);
  187.             }
  188.             else
  189.             {
  190.                 print("AIR");
  191.                 rb.useGravity = true;
  192.                 playerCollider.isTrigger = false;
  193.                 rb.AddForce(moveDir.normalized * moveSpeed * moveMultiplier * airMultiplier, ForceMode.Acceleration);
  194.             }
  195.         }
  196.     }
  197.  
  198.     void Jump()
  199.     {
  200.         rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
  201.     }
  202.  
  203.     void ControlDrag()
  204.     {
  205.         if (isGrounded)
  206.         {
  207.             rb.drag = groundDrag;
  208.         }
  209.         else
  210.         {
  211.             rb.drag = airDrag;
  212.         }
  213.     }
  214.  
  215.     void MyInput()
  216.     {
  217.         if (view.IsMine)
  218.         {
  219.             moveH = Input.GetAxisRaw("Horizontal");
  220.             moveV = Input.GetAxisRaw("Vertical");
  221.  
  222.             moveDir = transform.forward * moveV + transform.right * moveH;
  223.         }
  224.     }
  225.  
  226.     private void OnTriggerEnter(Collider other)
  227.     {
  228.         if (other.CompareTag("Shell"))
  229.         {
  230.             print("collided with tag Shell");
  231.             //Debug.Log("Game Over!");
  232.             Destroy(other.gameObject);
  233.  
  234.             if (view.IsMine)
  235.             {
  236.                 _gameOver.gameObject.SetActive(true);
  237.             }
  238.            
  239.         }
  240.     }
  241.  
  242.     public void Disconnect()
  243.     {
  244.         PhotonNetwork.LeaveRoom();
  245.         PhotonNetwork.LoadLevel("Lobby");
  246.     }
  247.  
  248.     public override void OnDisconnected(DisconnectCause cause)
  249.     {
  250.         Debug.Log("DISCONNECTED!");
  251.     }
  252. }
  253.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement