Added PlayerHealth and PlayerShoot

This commit is contained in:
2026-06-18 18:04:22 +03:00
parent c9c6c3e032
commit 87762d1721
19 changed files with 1600 additions and 754 deletions
@@ -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);
}
}