Advertisement
Dieton

PlayerScript

May 28th, 2023 (edited)
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.36 KB | Gaming | 0 0
  1. using Mirror;
  2. using UnityEngine;
  3. using TMPro;
  4. using Cinemachine;
  5. using UnityEngine.InputSystem;
  6.  
  7. namespace QuickStart
  8. {
  9.     public class PlayerScript : NetworkBehaviour
  10.     {
  11.  
  12.         private SceneScript sceneScript;
  13.  
  14.         //-- player movement --//
  15.         public float playerMoveSpeed = 2.0f;
  16.         Vector2 PlayerMovementInputVector;
  17.         float moveX;
  18.         float moveZ;
  19.  
  20.         //-- Input Variables --//
  21.         private PlayerInput playerInput;
  22.         private MyInputActions myInputActions;
  23.  
  24.         //-- About Players --//
  25.         public TMP_Text playerNameText;
  26.         public GameObject floatingInfo;
  27.  
  28.         private Material playerMaterialClone;
  29.  
  30.         //-- Network Stuff --//
  31.         [SyncVar(hook = nameof(OnNameChanged))]
  32.         public string playerName;
  33.  
  34.         [SyncVar(hook = nameof(OnColorChanged))]
  35.         public Color playerColor = Color.white;
  36.  
  37.         //-- wepon stuff --//
  38.         private int selectedWeaponLocal = 1;
  39.         public GameObject[] weaponArray;
  40.  
  41.         [SyncVar(hook = nameof(OnWeaponChanged))]
  42.         public int activeWeaponSynced = 1;
  43.  
  44.         private Weapon activeWeapon;
  45.         private float weaponCooldownTime;
  46.  
  47.         void Awake()
  48.         {
  49.             //allow all players to run this
  50.             sceneScript = GameObject.Find("SceneReference").GetComponent<SceneReference>().sceneScript;
  51.  
  52.             // disable all weapons
  53.             foreach (var item in weaponArray)
  54.                 if (item != null)
  55.                     item.SetActive(false);
  56.  
  57.             if (selectedWeaponLocal < weaponArray.Length && weaponArray[selectedWeaponLocal] != null)
  58.             {
  59.                 activeWeapon = weaponArray[selectedWeaponLocal].GetComponent<Weapon>();
  60.                 sceneScript.UIAmmo(activeWeapon.weaponAmmo);
  61.             }
  62.         }
  63.  
  64.         public void Start()
  65.         {
  66.             //NetworkServer.AddPlayerForConnection(conn, player, assetId);
  67.         }
  68.  
  69.         //==== when name changes display it to ui text ====//
  70.         void OnNameChanged(string _Old, string _New)
  71.         {
  72.             playerNameText.text = playerName;
  73.         }
  74.  
  75.         //==== when color changes apply to material ====//
  76.         void OnColorChanged(Color _Old, Color _New)
  77.         {
  78.             playerNameText.color = _New;
  79.             playerMaterialClone = new Material(GetComponent<Renderer>().material);
  80.             playerMaterialClone.color = _New;
  81.             GetComponent<Renderer>().material = playerMaterialClone;
  82.            
  83.         }
  84.  
  85.         //==== Local Player Only actions ====//
  86.         public override void OnStartLocalPlayer()
  87.         {
  88.             playerInput = GetComponent<PlayerInput>();
  89.             myInputActions = new MyInputActions();
  90.             myInputActions.Player.Enable();
  91.             myInputActions.Player.Fire.performed += ctx => Fire();
  92.             myInputActions.Player.Swap.performed += ctx => Swap();
  93.  
  94.             //add this script to scene script
  95.             sceneScript.playerScript = this;
  96.  
  97.             // Find player camera and tell it to look at this player
  98.             GameObject.Find("PlayerFollowCamera").GetComponent<CinemachineVirtualCamera>().Follow = this.gameObject.transform.GetChild(0).transform;
  99.             Camera.main.transform.SetParent(transform);
  100.             Camera.main.transform.localPosition = new Vector3(0, 0, 0);
  101.  
  102.             //Give the player a name and color
  103.             string name = "Player" + Random.Range(100, 999);
  104.             Color color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
  105.             CmdSetupPlayer(name, color);
  106.         }
  107.  
  108.         [Command]
  109.         public void CmdSendPlayerMessage()
  110.         {
  111.             if (sceneScript)
  112.                 sceneScript.statusText = $"{playerName} says hello {Random.Range(10, 99)}";
  113.         }
  114.  
  115.         [Command]
  116.         public void CmdSetupPlayer(string _name, Color _col)
  117.         {
  118.             //player info sent to server, then server updates sync vars which handles it on all clients
  119.             playerName = _name;
  120.             playerColor = _col;
  121.             sceneScript.statusText = $"{playerName} joined.";
  122.         }
  123.  
  124.         private void OnEnable()
  125.         {
  126.             /*
  127.             if (!isLocalPlayer)
  128.             {
  129.                 return;
  130.             }
  131.             */
  132.            // myInputActions.Enable();
  133.            
  134.         }
  135.  
  136.         private void OnDisable()
  137.         {
  138.             /*
  139.             if (!isLocalPlayer)
  140.             {
  141.                 return;
  142.             }
  143.             */
  144.             myInputActions.Disable();
  145.         }
  146.  
  147.         //==== Weapon Changeer ====//
  148.         void OnWeaponChanged(int _Old, int _New)
  149.         {
  150.             // disable old weapon
  151.             // in range and not null
  152.             if (0 < _Old && _Old < weaponArray.Length && weaponArray[_Old] != null)
  153.                 weaponArray[_Old].SetActive(false);
  154.  
  155.             // enable new weapon
  156.             // in range and not null
  157.             if (0 < _New && _New < weaponArray.Length && weaponArray[_New] != null)
  158.             {
  159.                 weaponArray[_New].SetActive(true);
  160.                 activeWeapon = weaponArray[activeWeaponSynced].GetComponent<Weapon>();
  161.                 if (isLocalPlayer)
  162.                     sceneScript.UIAmmo(activeWeapon.weaponAmmo);
  163.             }
  164.         }
  165.  
  166.         [Command]
  167.         public void CmdChangeActiveWeapon(int newIndex)
  168.         {
  169.             activeWeaponSynced = newIndex;
  170.         }
  171.  
  172.         void Update()
  173.         {
  174.             if (!isLocalPlayer)
  175.             {
  176.                 // make non-local players run this
  177.                 floatingInfo.transform.LookAt(Camera.main.transform);
  178.                 return;
  179.             }
  180.  
  181.            
  182.         }
  183.  
  184.         private void LateUpdate()
  185.         {
  186.             if (!isLocalPlayer)
  187.             {
  188.                 return;
  189.             }
  190.             PlayerMove();
  191.         }
  192.  
  193.         //==== Player Move ====//
  194.         public void PlayerMove()
  195.         {
  196.             if (myInputActions != null)
  197.             {
  198.                 PlayerMovementInputVector = myInputActions.Player.Move.ReadValue<Vector2>();
  199.                 moveX = PlayerMovementInputVector.x * Time.deltaTime * 110.0f;// _input.move.x
  200.                 moveZ = PlayerMovementInputVector.y * Time.deltaTime * playerMoveSpeed;//_input.move.y
  201.                 transform.Translate(0, 0, moveZ);
  202.                 transform.Rotate(0, moveX, 0);
  203.             }
  204.            
  205.         }
  206.  
  207.         public void Fire()//InputAction.CallbackContext context) {
  208.         {
  209.             if (activeWeapon && Time.time > weaponCooldownTime && activeWeapon.weaponAmmo > 0)
  210.             {
  211.                 weaponCooldownTime = Time.time + activeWeapon.weaponCooldown;
  212.                 activeWeapon.weaponAmmo -= 1;
  213.                 sceneScript.UIAmmo(activeWeapon.weaponAmmo);
  214.                 CmdShootRay();
  215.             }
  216.         }
  217.  
  218.         public void Swap() {
  219.             selectedWeaponLocal += 1;
  220.  
  221.             if (selectedWeaponLocal > weaponArray.Length)
  222.                 selectedWeaponLocal = 1;
  223.  
  224.             CmdChangeActiveWeapon(selectedWeaponLocal);
  225.         }
  226.  
  227.         [Command]
  228.         void CmdShootRay()
  229.         {
  230.             RpcFireWeapon();
  231.         }
  232.  
  233.         [ClientRpc]
  234.         void RpcFireWeapon()
  235.         {
  236.             //bulletAudio.Play(); muzzleflash  etc
  237.             GameObject bullet = Instantiate(activeWeapon.weaponBullet, activeWeapon.weaponFirePosition.position, activeWeapon.weaponFirePosition.rotation);
  238.             bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * activeWeapon.weaponSpeed;
  239.             Destroy(bullet, activeWeapon.weaponLife);
  240.         }
  241.  
  242.         public override void OnStartServer()
  243.         {
  244.             base.OnStartServer();
  245.             Debugger("On Start Server" + playerName);
  246.         }
  247.  
  248.         public override void OnStartClient()
  249.         {
  250.             base.OnStartClient();
  251.             Debugger("On Start Client - " + playerName);
  252.         }
  253.  
  254.         public override void OnStartAuthority()
  255.         {
  256.             base.OnStartAuthority();
  257.             Debugger("On Start Authority - " + playerName);
  258.         }
  259.  
  260.         public void Debugger(object debugInfo) {
  261.             sceneScript.debugtext.text = sceneScript.debugtext.text + "\n\nplayer: " + debugInfo.ToString();
  262.             Debug.Log("player: " + debugInfo);
  263.         }
  264.     }
  265. }
Tags: Unity mirror
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement