Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Mirror;
- using UnityEngine;
- using TMPro;
- using Cinemachine;
- using UnityEngine.InputSystem;
- namespace QuickStart
- {
- public class PlayerScript : NetworkBehaviour
- {
- private SceneScript sceneScript;
- //-- player movement --//
- public float playerMoveSpeed = 2.0f;
- Vector2 PlayerMovementInputVector;
- float moveX;
- float moveZ;
- //-- Input Variables --//
- private PlayerInput playerInput;
- private MyInputActions myInputActions;
- //-- About Players --//
- public TMP_Text playerNameText;
- public GameObject floatingInfo;
- private Material playerMaterialClone;
- //-- Network Stuff --//
- [SyncVar(hook = nameof(OnNameChanged))]
- public string playerName;
- [SyncVar(hook = nameof(OnColorChanged))]
- public Color playerColor = Color.white;
- //-- wepon stuff --//
- private int selectedWeaponLocal = 1;
- public GameObject[] weaponArray;
- [SyncVar(hook = nameof(OnWeaponChanged))]
- public int activeWeaponSynced = 1;
- private Weapon activeWeapon;
- private float weaponCooldownTime;
- void Awake()
- {
- //allow all players to run this
- sceneScript = GameObject.Find("SceneReference").GetComponent<SceneReference>().sceneScript;
- // 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.UIAmmo(activeWeapon.weaponAmmo);
- }
- }
- public void Start()
- {
- //NetworkServer.AddPlayerForConnection(conn, player, assetId);
- }
- //==== when name changes display it to ui text ====//
- void OnNameChanged(string _Old, string _New)
- {
- playerNameText.text = playerName;
- }
- //==== when color changes apply to material ====//
- void OnColorChanged(Color _Old, Color _New)
- {
- playerNameText.color = _New;
- playerMaterialClone = new Material(GetComponent<Renderer>().material);
- playerMaterialClone.color = _New;
- GetComponent<Renderer>().material = playerMaterialClone;
- }
- //==== Local Player Only actions ====//
- public override void OnStartLocalPlayer()
- {
- playerInput = GetComponent<PlayerInput>();
- myInputActions = new MyInputActions();
- myInputActions.Player.Enable();
- myInputActions.Player.Fire.performed += ctx => Fire();
- myInputActions.Player.Swap.performed += ctx => Swap();
- //add this script to scene script
- sceneScript.playerScript = this;
- // Find player camera and tell it to look at this player
- GameObject.Find("PlayerFollowCamera").GetComponent<CinemachineVirtualCamera>().Follow = this.gameObject.transform.GetChild(0).transform;
- Camera.main.transform.SetParent(transform);
- Camera.main.transform.localPosition = new Vector3(0, 0, 0);
- //Give the player a name and color
- string name = "Player" + Random.Range(100, 999);
- Color color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
- CmdSetupPlayer(name, color);
- }
- [Command]
- public void CmdSendPlayerMessage()
- {
- if (sceneScript)
- sceneScript.statusText = $"{playerName} says hello {Random.Range(10, 99)}";
- }
- [Command]
- public void CmdSetupPlayer(string _name, Color _col)
- {
- //player info sent to server, then server updates sync vars which handles it on all clients
- playerName = _name;
- playerColor = _col;
- sceneScript.statusText = $"{playerName} joined.";
- }
- private void OnEnable()
- {
- /*
- if (!isLocalPlayer)
- {
- return;
- }
- */
- // myInputActions.Enable();
- }
- private void OnDisable()
- {
- /*
- if (!isLocalPlayer)
- {
- return;
- }
- */
- myInputActions.Disable();
- }
- //==== Weapon Changeer ====//
- 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>();
- if (isLocalPlayer)
- sceneScript.UIAmmo(activeWeapon.weaponAmmo);
- }
- }
- [Command]
- public void CmdChangeActiveWeapon(int newIndex)
- {
- activeWeaponSynced = newIndex;
- }
- void Update()
- {
- if (!isLocalPlayer)
- {
- // make non-local players run this
- floatingInfo.transform.LookAt(Camera.main.transform);
- return;
- }
- }
- private void LateUpdate()
- {
- if (!isLocalPlayer)
- {
- return;
- }
- PlayerMove();
- }
- //==== Player Move ====//
- public void PlayerMove()
- {
- if (myInputActions != null)
- {
- PlayerMovementInputVector = myInputActions.Player.Move.ReadValue<Vector2>();
- moveX = PlayerMovementInputVector.x * Time.deltaTime * 110.0f;// _input.move.x
- moveZ = PlayerMovementInputVector.y * Time.deltaTime * playerMoveSpeed;//_input.move.y
- transform.Translate(0, 0, moveZ);
- transform.Rotate(0, moveX, 0);
- }
- }
- public void Fire()//InputAction.CallbackContext context) {
- {
- if (activeWeapon && Time.time > weaponCooldownTime && activeWeapon.weaponAmmo > 0)
- {
- weaponCooldownTime = Time.time + activeWeapon.weaponCooldown;
- activeWeapon.weaponAmmo -= 1;
- sceneScript.UIAmmo(activeWeapon.weaponAmmo);
- CmdShootRay();
- }
- }
- public void Swap() {
- selectedWeaponLocal += 1;
- if (selectedWeaponLocal > weaponArray.Length)
- selectedWeaponLocal = 1;
- CmdChangeActiveWeapon(selectedWeaponLocal);
- }
- [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);
- }
- public override void OnStartServer()
- {
- base.OnStartServer();
- Debugger("On Start Server" + playerName);
- }
- public override void OnStartClient()
- {
- base.OnStartClient();
- Debugger("On Start Client - " + playerName);
- }
- public override void OnStartAuthority()
- {
- base.OnStartAuthority();
- Debugger("On Start Authority - " + playerName);
- }
- public void Debugger(object debugInfo) {
- sceneScript.debugtext.text = sceneScript.debugtext.text + "\n\nplayer: " + debugInfo.ToString();
- Debug.Log("player: " + debugInfo);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement