Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Unity.Netcode;
- using UnityEngine.InputSystem;
- using Cinemachine;
- using System;
- using TMPro;
- public class NetPlayer : NetworkBehaviour
- {
- public static event EventHandler OnAnyPlayerSpawned;
- //-- Player --//
- //public static NetPlayer netPlayerLocalInstance { get; private set; }
- //-- Player Name --//
- //public NetworkVariable<string> playerName = new NetworkVariable<string>("Player Name");
- public string playerName = "player name";
- //-- player Info Text Display --//
- private List<string> names = new List<string>
- {
- "Riderch" ,
- "Trent",
- "Lydia",
- "Jenkins",
- "Lelia",
- "Ramsey",
- "Russ",
- "Schroeder"
- };
- public TMP_Text playerNameText;
- //-- player render mesh --//
- public List<GameObject> playerRendermesh;
- // -- Animation --//
- public Animator anim;
- //-- Camera System --//
- public Transform lookPoint;
- //-- Input System --//
- //private PlayerInput playerInput;
- private MyInputActions myInputActions;
- private MyInputActions MyInputActionsGet
- {
- get
- {
- if (myInputActions != null) { return myInputActions; }
- return myInputActions = new MyInputActions();
- }
- }
- public CharacterController playerController;
- private float lookSpeed = 500.0f;
- Vector2 mouseAround;
- private float playerSpeed = 2.0f;
- private float gravity = 0.8f;
- private float realTime;
- //-- Network Development Test --//
- //public NetworkVariable<int> networkTestInt = new NetworkVariable<int>(0);
- // Start is called before the first frame update
- void Start()
- {
- //display player name over head
- //playerName = playerName + (int)UnityEngine.Random.Range(10, 99);
- // give this player a name
- //name = playerName;
- //send name to debugger
- //MyDebugger.myDebugger.Log(playerName);
- //MyDebugger.myDebugger.Warning("More");
- //MyDebugger.myDebugger.Error("STOP");
- if (!IsOwner)
- {
- //playerNameText.text = playerName;
- return;
- }
- //if is owner hide text
- playerNameText.gameObject.SetActive(false);
- //hide player mesh
- if (playerRendermesh.Count > 0)
- {
- for (int i = 0; i < playerRendermesh.Count; i++)
- {
- playerRendermesh[i].SetActive(false);
- }
- }
- //get player camera and atach to player
- GameObject.Find("playerCamera").GetComponent<CinemachineVirtualCamera>().Follow = lookPoint;
- PlayerInputSetup();
- }
- //On network player spawn start
- public override void OnNetworkSpawn()
- {
- base.OnNetworkSpawn();
- if (IsOwner)
- {
- //netPlayerLocalInstance = this;
- }
- OnAnyPlayerSpawned?.Invoke(this, EventArgs.Empty);
- //networkTestInt.OnValueChanged += NetwrokDesTestFun;
- //networkTestInt.Value = UnityEngine.Random.Range(10, 99);
- //playerName.OnValueChanged += NetworkPlayernameChange;
- //playerName.Value = names[UnityEngine.Random.Range(0, names.Count)];
- // give this player a name
- }
- /*
- private void NetworkPlayernameChange(string previusValue, string newValue)
- {
- name = playerName.Value + networkTestInt;
- }
- private void NetwrokDesTestFun(int previusValue, int newValue) {
- //networkTestInt
- }
- */
- //==== Input Sytem ====//
- public void PlayerInputSetup()
- {
- //playerInput = GetComponent<PlayerInput>();
- myInputActions = new MyInputActions();
- myInputActions.Player.Enable();
- }
- private void OnEnable() => MyInputActionsGet.Enable();
- private void OnDisable() => MyInputActionsGet.Disable();
- // Update is called once per frame
- void Update()
- {
- realTime = Time.deltaTime;
- if (!IsOwner)
- {
- playerNameText.gameObject.transform.LookAt(2 * playerNameText.gameObject.transform.position - Camera.main.transform.position);
- return;
- }
- PlayerMove();
- }
- //==== Player Move ====//
- public void PlayerMove()
- {
- //---- Player Movement ----//
- Vector2 PlayerMovementInputVector = MyInputActionsGet.Player.Move.ReadValue<Vector2>();
- Vector2 playerMouseMoveInputVector = MyInputActionsGet.Player.Look.ReadValue<Vector2>();
- float moveX = PlayerMovementInputVector.x;
- float moveZ = PlayerMovementInputVector.y;
- Vector3 direction = transform.TransformDirection(PlayerMovementInputVector.x, -gravity, PlayerMovementInputVector.y);
- playerController.Move(direction * realTime * playerSpeed);
- transform.Rotate(0, playerMouseMoveInputVector.x * realTime * lookSpeed, 0);
- //---- Player Animation ----//
- anim.SetFloat("Speed", PlayerMovementInputVector.y);
- //---- Camera movement ----//
- mouseAround.x += playerMouseMoveInputVector.x * lookSpeed * realTime;
- mouseAround.y += playerMouseMoveInputVector.y * lookSpeed * realTime;
- float pitchRotation = ClampAngle(mouseAround.y, 70, -70);
- lookPoint.rotation = Quaternion.Euler(pitchRotation , transform.eulerAngles.y, transform.eulerAngles.z);
- }
- //==== Clamp Angle ====//
- private static float ClampAngle(float angle, float max, float min)
- {
- if (angle < -360f) angle += 360f;
- if (angle > 360f) angle -= 360f;
- return Mathf.Clamp(angle, min, max);
- }
- public NetworkObject GetNetwokObject() {
- return NetworkObject;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement