Added PlayerHealth and PlayerShoot
This commit is contained in:
@@ -16,14 +16,14 @@ public class MainMenuUIController : MonoBehaviour
|
||||
|
||||
private void HostGame()
|
||||
{
|
||||
Debug.Log("Hosting game...");
|
||||
NetworkManager.Singleton.StartHost();
|
||||
NetworkManager.Singleton.SceneManager.LoadScene("DEV-Game", LoadSceneMode.Single);
|
||||
Debug.Log("Hosting game...");
|
||||
}
|
||||
|
||||
private void JoinGame()
|
||||
{
|
||||
Debug.Log("Joining game...");
|
||||
NetworkManager.Singleton.StartClient();
|
||||
Debug.Log("Joining game...");
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -5,47 +5,70 @@ using Unity.Collections;
|
||||
|
||||
public class PlayerController : NetworkBehaviour
|
||||
{
|
||||
[SerializeField] private TMP_InputField PlayerNameInputField;
|
||||
[SerializeField] private TMP_Text PlayerNameText;
|
||||
[SerializeField] private float moveSpeed = 5f;
|
||||
|
||||
public NetworkVariable<FixedString128Bytes> PlayerName = new NetworkVariable<FixedString128Bytes>(
|
||||
"Джон Бляк",
|
||||
NetworkVariableReadPermission.Everyone,
|
||||
NetworkVariableWritePermission.Owner
|
||||
);
|
||||
[SerializeField] private Camera playerCamera;
|
||||
|
||||
[SerializeField] private float mouseSensitivity = 2f;
|
||||
[SerializeField] private Transform cameraRoot;
|
||||
|
||||
float verticalRotation = 0f;
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
PlayerName.OnValueChanged += OnPlayerNameChanged;
|
||||
SetPlayerNameText();
|
||||
if (playerCamera != null)
|
||||
{
|
||||
playerCamera.enabled = IsOwner;
|
||||
playerCamera.GetComponent<AudioListener>().enabled = IsOwner;
|
||||
}
|
||||
|
||||
if (!IsOwner) return;
|
||||
if (IsOwner)
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
}
|
||||
|
||||
this.transform.position = new Vector3(Random.Range(-10f, 10f), 1f, Random.Range(-10f, 10f));
|
||||
PlayerNameInputField.onValueChanged.AddListener(OnPlayerNameInputChanged);
|
||||
if (IsOwner)
|
||||
{
|
||||
GetComponent<Renderer>().material.color = Color.blue;
|
||||
}
|
||||
else
|
||||
{
|
||||
GetComponent<Renderer>().material.color = Color.red;
|
||||
}
|
||||
|
||||
if (IsOwner)
|
||||
{
|
||||
transform.position = new Vector3(Random.Range(-10f, 10f), 1f, Random.Range(-10f, 10f));
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPlayerNameChanged(FixedString128Bytes previousValue, FixedString128Bytes newValue)
|
||||
public void Update()
|
||||
{
|
||||
Debug.Log($"Player name changed from {previousValue} to {newValue}");
|
||||
SetPlayerNameText();
|
||||
if(!IsOwner) return;
|
||||
|
||||
HandleMovement();
|
||||
HandleRotation();
|
||||
}
|
||||
|
||||
private void OnPlayerNameInputChanged(string value)
|
||||
private void HandleMovement()
|
||||
{
|
||||
Debug.Log($"Player name input changed to {value}");
|
||||
SetPlayerNameText();
|
||||
float _moveX = Input.GetAxis("Horizontal");
|
||||
float _moveZ = Input.GetAxis("Vertical");
|
||||
|
||||
Vector3 move = transform.right * _moveX + transform.forward * _moveZ;
|
||||
transform.position += move * moveSpeed * Time.deltaTime;
|
||||
}
|
||||
|
||||
public override void OnNetworkDespawn()
|
||||
private void HandleRotation()
|
||||
{
|
||||
PlayerName.OnValueChanged -= OnPlayerNameChanged;
|
||||
if(IsOwner)
|
||||
PlayerNameInputField.onValueChanged.RemoveListener(OnPlayerNameInputChanged);
|
||||
}
|
||||
float _mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
|
||||
float _mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;
|
||||
|
||||
private void SetPlayerNameText()
|
||||
{
|
||||
PlayerNameText.text = PlayerName.Value.ToString();
|
||||
transform.Rotate(Vector3.up * _mouseX);
|
||||
|
||||
verticalRotation -= _mouseY;
|
||||
verticalRotation = Mathf.Clamp(verticalRotation, -90f, 90f);
|
||||
cameraRoot.localRotation = Quaternion.Euler(verticalRotation, 0f, 0f);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
using UnityEngine;
|
||||
using Unity.Netcode;
|
||||
using TMPro;
|
||||
|
||||
public class PlayerHealth : NetworkBehaviour
|
||||
{
|
||||
[SerializeField] private int maxHealth = 100;
|
||||
[SerializeField] private TMP_Text healthText;
|
||||
|
||||
public NetworkVariable<int> currentHealth = new NetworkVariable<int>(
|
||||
100,
|
||||
NetworkVariableReadPermission.Everyone,
|
||||
NetworkVariableWritePermission.Owner
|
||||
);
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
currentHealth.OnValueChanged += OnHealthChanged;
|
||||
|
||||
if (IsServer)
|
||||
{
|
||||
currentHealth.Value = maxHealth;
|
||||
healthText.text = $"Health: {maxHealth}";
|
||||
}
|
||||
}
|
||||
|
||||
private void OnHealthChanged(int _previousValue, int _newValue)
|
||||
{
|
||||
Debug.Log($"Health changed from {_previousValue} to {_newValue}");
|
||||
|
||||
healthText.text = $"Health: {_newValue}";
|
||||
}
|
||||
|
||||
public void TakeDamage(int _damageAmount)
|
||||
{
|
||||
if (!IsServer) return;
|
||||
|
||||
currentHealth.Value -= _damageAmount;
|
||||
|
||||
if (currentHealth.Value <= 0)
|
||||
{
|
||||
Die();
|
||||
}
|
||||
}
|
||||
|
||||
private void Die()
|
||||
{
|
||||
OnDiedClientRpc();
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
private void OnDiedClientRpc()
|
||||
{
|
||||
Debug.Log($"Player {OwnerClientId} died");
|
||||
gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
public override void OnNetworkDespawn()
|
||||
{
|
||||
currentHealth.OnValueChanged -= OnHealthChanged;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 89a166e74678da94ba61fa76b7f4a790
|
||||
@@ -0,0 +1,49 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
public class PlayerShoot : NetworkBehaviour
|
||||
{
|
||||
[SerializeField] float damageAmount = 10f;
|
||||
[SerializeField] float range = 100f;
|
||||
[SerializeField] Transform cameraRoot;
|
||||
|
||||
[SerializeField] GameObject hitEffectPrefab;
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (!IsOwner) return;
|
||||
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
ShootServerRpc(cameraRoot.position, cameraRoot.forward);
|
||||
}
|
||||
}
|
||||
|
||||
[ServerRpc]
|
||||
private void ShootServerRpc(Vector3 _origin, Vector3 _direction)
|
||||
{
|
||||
Collider[] ownerColliders = GetComponentsInChildren<Collider>();
|
||||
|
||||
if (Physics.Raycast(_origin, _direction, out RaycastHit _hit, range))
|
||||
{
|
||||
if (System.Array.Exists(ownerColliders, c => c == _hit.collider))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_hit.collider.TryGetComponent(out PlayerHealth _playerHealth))
|
||||
{
|
||||
_playerHealth.TakeDamage((int)damageAmount);
|
||||
}
|
||||
|
||||
SpawnHitEffectClientRpc(_hit.point);
|
||||
}
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
private void SpawnHitEffectClientRpc(Vector3 _hitPosition)
|
||||
{
|
||||
Debug.Log($"Spawning hit effect at {_hitPosition}");
|
||||
NetworkBehaviour.Instantiate(hitEffectPrefab, _hitPosition, Quaternion.identity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a8abb30d1d764eb41b7bb2555d480955
|
||||
@@ -0,0 +1,10 @@
|
||||
using UnityEngine;
|
||||
using Unity.Netcode;
|
||||
|
||||
public class PlayerUIController : NetworkBehaviour
|
||||
{
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
GetComponent<Canvas>().enabled = IsOwner;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: df6776ae1b2a9ea4faec1e9006289f29
|
||||
Reference in New Issue
Block a user