Advertisement
Dieton

NetPlayer

Jun 12th, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.78 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Unity.Netcode;
  5. using UnityEngine.InputSystem;
  6. using Cinemachine;
  7. using System;
  8. using TMPro;
  9.  
  10. public class NetPlayer : NetworkBehaviour
  11. {
  12.  
  13.     public static event EventHandler OnAnyPlayerSpawned;
  14.     //-- Player --//
  15.     //public static NetPlayer netPlayerLocalInstance { get; private set; }
  16.     //-- Player Name --//
  17.     //public NetworkVariable<string> playerName = new NetworkVariable<string>("Player Name");
  18.     public string playerName = "player name";
  19.     //-- player Info Text Display --//
  20.     private List<string> names = new List<string>
  21.     {
  22.         "Riderch" ,
  23.         "Trent",
  24.         "Lydia",
  25.         "Jenkins",
  26.         "Lelia",
  27.         "Ramsey",
  28.         "Russ",
  29.         "Schroeder"
  30.     };
  31.  
  32.     public TMP_Text playerNameText;
  33.  
  34.     //-- player render mesh --//
  35.     public List<GameObject> playerRendermesh;
  36.  
  37.     // -- Animation --//
  38.     public Animator anim;
  39.  
  40.     //-- Camera System --//
  41.     public Transform lookPoint;
  42.  
  43.     //-- Input System --//
  44.     //private PlayerInput playerInput;
  45.     private MyInputActions myInputActions;
  46.     private MyInputActions MyInputActionsGet
  47.     {
  48.         get
  49.         {
  50.             if (myInputActions != null) { return myInputActions; }
  51.             return myInputActions = new MyInputActions();
  52.         }
  53.     }
  54.     public CharacterController playerController;
  55.     private float lookSpeed = 500.0f;
  56.     Vector2 mouseAround;
  57.     private float playerSpeed = 2.0f;
  58.     private float gravity = 0.8f;
  59.     private float realTime;
  60.  
  61.     //-- Network Development Test --//
  62.     //public NetworkVariable<int> networkTestInt = new NetworkVariable<int>(0);
  63.  
  64.     // Start is called before the first frame update
  65.     void Start()
  66.     {
  67.         //display player name over head
  68.         //playerName = playerName + (int)UnityEngine.Random.Range(10, 99);
  69.  
  70.         // give this player a name
  71.         //name = playerName;
  72.  
  73.         //send name to debugger
  74.         //MyDebugger.myDebugger.Log(playerName);
  75.  
  76.         //MyDebugger.myDebugger.Warning("More");
  77.         //MyDebugger.myDebugger.Error("STOP");
  78.  
  79.         if (!IsOwner)
  80.         {
  81.             //playerNameText.text = playerName;
  82.             return;
  83.         }
  84.  
  85.         //if is owner hide text
  86.         playerNameText.gameObject.SetActive(false);
  87.  
  88.         //hide player mesh
  89.         if (playerRendermesh.Count > 0)
  90.         {
  91.             for (int i = 0; i < playerRendermesh.Count; i++)
  92.             {
  93.                 playerRendermesh[i].SetActive(false);
  94.             }
  95.         }
  96.        
  97.  
  98.         //get player camera and atach to player
  99.         GameObject.Find("playerCamera").GetComponent<CinemachineVirtualCamera>().Follow = lookPoint;
  100.  
  101.         PlayerInputSetup();
  102.     }
  103.  
  104.     //On network player spawn start
  105.     public override void OnNetworkSpawn()
  106.     {
  107.         base.OnNetworkSpawn();
  108.         if (IsOwner)
  109.         {
  110.             //netPlayerLocalInstance = this;
  111.         }
  112.  
  113.         OnAnyPlayerSpawned?.Invoke(this, EventArgs.Empty);
  114.  
  115.  
  116.         //networkTestInt.OnValueChanged += NetwrokDesTestFun;
  117.         //networkTestInt.Value = UnityEngine.Random.Range(10, 99);
  118.  
  119.         //playerName.OnValueChanged += NetworkPlayernameChange;
  120.         //playerName.Value = names[UnityEngine.Random.Range(0, names.Count)];
  121.  
  122.         // give this player a name
  123.        
  124.     }
  125.     /*
  126.     private void NetworkPlayernameChange(string previusValue, string newValue)
  127.     {
  128.         name = playerName.Value + networkTestInt;
  129.     }
  130.  
  131.     private void NetwrokDesTestFun(int previusValue, int newValue) {
  132.         //networkTestInt
  133.     }
  134.     */
  135.     //==== Input Sytem ====//
  136.     public void PlayerInputSetup()
  137.     {
  138.         //playerInput = GetComponent<PlayerInput>();
  139.         myInputActions = new MyInputActions();
  140.         myInputActions.Player.Enable();
  141.     }
  142.  
  143.     private void OnEnable() => MyInputActionsGet.Enable();
  144.  
  145.     private void OnDisable() => MyInputActionsGet.Disable();
  146.  
  147.     // Update is called once per frame
  148.     void Update()
  149.     {
  150.         realTime = Time.deltaTime;
  151.  
  152.         if (!IsOwner)
  153.         {
  154.             playerNameText.gameObject.transform.LookAt(2 * playerNameText.gameObject.transform.position - Camera.main.transform.position);
  155.             return;
  156.         }
  157.         PlayerMove();
  158.     }
  159.  
  160.     //==== Player Move ====//
  161.     public void PlayerMove()
  162.     {
  163.         //---- Player Movement ----//
  164.         Vector2 PlayerMovementInputVector = MyInputActionsGet.Player.Move.ReadValue<Vector2>();
  165.         Vector2 playerMouseMoveInputVector = MyInputActionsGet.Player.Look.ReadValue<Vector2>();
  166.         float moveX = PlayerMovementInputVector.x;
  167.         float moveZ = PlayerMovementInputVector.y;
  168.         Vector3 direction = transform.TransformDirection(PlayerMovementInputVector.x, -gravity, PlayerMovementInputVector.y);
  169.         playerController.Move(direction * realTime * playerSpeed);
  170.         transform.Rotate(0, playerMouseMoveInputVector.x * realTime * lookSpeed, 0);
  171.         //---- Player Animation ----//
  172.         anim.SetFloat("Speed", PlayerMovementInputVector.y);
  173.  
  174.         //---- Camera movement ----//
  175.         mouseAround.x += playerMouseMoveInputVector.x * lookSpeed * realTime;
  176.         mouseAround.y += playerMouseMoveInputVector.y * lookSpeed * realTime;
  177.         float pitchRotation = ClampAngle(mouseAround.y, 70, -70);
  178.         lookPoint.rotation = Quaternion.Euler(pitchRotation , transform.eulerAngles.y, transform.eulerAngles.z);
  179.     }
  180.  
  181.     //==== Clamp Angle ====//
  182.     private static float ClampAngle(float angle, float max, float min)
  183.     {
  184.         if (angle < -360f) angle += 360f;
  185.         if (angle > 360f) angle -= 360f;
  186.         return Mathf.Clamp(angle, min, max);
  187.     }
  188.  
  189.     public NetworkObject GetNetwokObject() {
  190.         return NetworkObject;
  191.     }
  192. }
  193.  
Tags: Unity Netcode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement