Added Fog and other fixes
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
[DefaultExecutionOrder(100)] // после PlayerMovement.LateUpdate (pitch камеры)
|
||||
public class PlayerShoot : NetworkBehaviour
|
||||
{
|
||||
[SerializeField] float damageAmount = 10f;
|
||||
@@ -24,7 +25,7 @@ public class PlayerShoot : NetworkBehaviour
|
||||
weaponView = GetComponentInChildren<WeaponView>(true);
|
||||
}
|
||||
|
||||
void Update()
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (!isActive) return;
|
||||
|
||||
@@ -33,40 +34,63 @@ public class PlayerShoot : NetworkBehaviour
|
||||
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
Vector3 _origin = cameraRoot.position;
|
||||
Vector3 _direction = cameraRoot.forward;
|
||||
|
||||
weaponView?.PlayShoot();
|
||||
ShootServerRpc(cameraRoot.position, cameraRoot.forward);
|
||||
|
||||
// Мгновенный фидбек на стреляющем клиенте — не ждём серверный ClientRpc
|
||||
if (TryRaycastShot(_origin, _direction, out RaycastHit _predictedHit))
|
||||
SpawnHitEffectLocal(_predictedHit.point);
|
||||
|
||||
ShootServerRpc(_origin, _direction);
|
||||
}
|
||||
}
|
||||
|
||||
[ServerRpc]
|
||||
private void ShootServerRpc(Vector3 _origin, Vector3 _direction)
|
||||
{
|
||||
// Записываем выстрел для клона — ровно то, что пришло в RPC
|
||||
GetComponent<GhostRecorder>()?.RecordShot(_origin, _direction);
|
||||
|
||||
Collider[] ownerColliders = GetComponentsInChildren<Collider>();
|
||||
if (!TryRaycastShot(_origin, _direction, out RaycastHit _hit))
|
||||
return;
|
||||
|
||||
if (Physics.Raycast(_origin, _direction, out RaycastHit _hit, range))
|
||||
{
|
||||
if (System.Array.Exists(ownerColliders, c => c == _hit.collider))
|
||||
{
|
||||
return;
|
||||
}
|
||||
PlayerHealth _targetHealth = _hit.collider.GetComponentInParent<PlayerHealth>();
|
||||
if (_targetHealth != null)
|
||||
_targetHealth.TakeDamage((int)damageAmount);
|
||||
|
||||
PlayerHealth _playerHealth = _hit.collider.GetComponentInParent<PlayerHealth>();
|
||||
if (_playerHealth != null)
|
||||
{
|
||||
_playerHealth.TakeDamage((int)damageAmount);
|
||||
}
|
||||
|
||||
SpawnHitEffectClientRpc(_hit.point);
|
||||
}
|
||||
SpawnHitEffectClientRpc(_hit.point);
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
private void SpawnHitEffectClientRpc(Vector3 _hitPosition)
|
||||
{
|
||||
// Debug.Log($"Spawning hit effect at {_hitPosition}");
|
||||
// Владелец уже показал эффект локально при клике
|
||||
if (IsOwner) return;
|
||||
|
||||
SpawnHitEffectLocal(_hitPosition);
|
||||
}
|
||||
|
||||
private bool TryRaycastShot(Vector3 _origin, Vector3 _direction, out RaycastHit _hit)
|
||||
{
|
||||
_hit = default;
|
||||
|
||||
if (!Physics.Raycast(_origin, _direction, out _hit, range))
|
||||
return false;
|
||||
|
||||
Collider[] _ownerColliders = GetComponentsInChildren<Collider>();
|
||||
for (int i = 0; i < _ownerColliders.Length; i++)
|
||||
{
|
||||
if (_ownerColliders[i] == _hit.collider)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void SpawnHitEffectLocal(Vector3 _hitPosition)
|
||||
{
|
||||
if (hitEffectPrefab == null) return;
|
||||
Instantiate(hitEffectPrefab, _hitPosition, Quaternion.identity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user