Commit 38024f9f by alsunj

Merge branch 'feat/sync-destructables' into 'main'

fix health sync

See merge request alsunj/loputoo!6
parents 39903de7 1206956c
using System;
using DG.Tweening;
using Unity.Netcode;
using UnityEngine;
public class Barrel : MonoBehaviour, IDestrucable
public class Barrel : NetworkBehaviour, IDestrucable
{
[SerializeField] private DestructableSettings _destructableSettings;
private float _health;
private readonly NetworkVariable<float> _health = new NetworkVariable<float>();
private void Start()
public override void OnNetworkSpawn()
{
_health = _destructableSettings.health;
if (IsServer)
{
_health.Value = _destructableSettings.health;
}
_health.OnValueChanged += OnHealthChanged;
}
public override void OnDestroy()
{
_health.OnValueChanged -= OnHealthChanged;
}
public void TakeDamage(float damage)
private void OnHealthChanged(float oldHealth, float newHealth)
{
_health -= damage;
Debug.Log(_health);
if (_health <= 0)
if (newHealth <= 0)
{
transform.DOScale(Vector3.zero, 0.5f).SetDelay(_destructableSettings.hitDelay)
.OnComplete(() => gameObject.SetActive(false));
......@@ -30,4 +36,52 @@ public class Barrel : MonoBehaviour, IDestrucable
.SetDelay(_destructableSettings.hitDelay);
}
}
[ClientRpc]
private void RpcShowBarrelDestroyForEveryOneClientRpc()
{
transform.DOScale(Vector3.zero, 0.5f).SetDelay(_destructableSettings.hitDelay)
.OnComplete(() => gameObject.SetActive(false));
}
[ClientRpc]
private void RpcShowBarrelDamageForEveryoneClientRpc()
{
transform.DOShakePosition(0.5f, _destructableSettings.shakeOffset, 10, 90, false, true)
.SetDelay(_destructableSettings.hitDelay);
}
public void TakeDamage(float damage)
{
RequestTakeDamageServerRpc(damage);
}
[ServerRpc(RequireOwnership = false)]
private void RequestTakeDamageServerRpc(float damage)
{
_health.Value -= damage;
Debug.Log(_health.Value);
Debug.Log("health value at checking: " + _health.Value);
if (_health.Value <= 0)
{
CmdShowBarrelDestroyForEveryOneServerRpc();
}
else
{
CmdShowBarrelDamageForEveryoneServerRpc();
}
}
[ServerRpc(RequireOwnership = false)]
private void CmdShowBarrelDestroyForEveryOneServerRpc()
{
RpcShowBarrelDestroyForEveryOneClientRpc();
}
[ServerRpc(RequireOwnership = false)]
private void CmdShowBarrelDamageForEveryoneServerRpc()
{
RpcShowBarrelDamageForEveryoneClientRpc();
}
}
\ No newline at end of file
......@@ -375,6 +375,7 @@ public class PlayerController : NetworkBehaviour
_playerManager.playerEvents.PlayerAttack();
}
}
private void RotatePlayerTowardsTarget(Collider hit)
{
Vector3 direction = (hit.transform.position - transform.position).normalized;
......
......@@ -97,10 +97,10 @@ PrefabInstance:
addedObject: {fileID: 6303177976271785865}
- targetCorrespondingSourceObject: {fileID: 919132149155446097, guid: e13481dedf01cb048b93d9e493c354b4, type: 3}
insertIndex: -1
addedObject: {fileID: 5127824732359231213}
addedObject: {fileID: 1580681325717503087}
- targetCorrespondingSourceObject: {fileID: 919132149155446097, guid: e13481dedf01cb048b93d9e493c354b4, type: 3}
insertIndex: -1
addedObject: {fileID: 1580681325717503087}
addedObject: {fileID: 5127824732359231213}
m_SourcePrefab: {fileID: 100100000, guid: e13481dedf01cb048b93d9e493c354b4, type: 3}
--- !u!1 &8106328193517419873 stripped
GameObject:
......@@ -129,19 +129,6 @@ MeshCollider:
m_Convex: 0
m_CookingOptions: 30
m_Mesh: {fileID: -4131521360128221038, guid: e13481dedf01cb048b93d9e493c354b4, type: 3}
--- !u!114 &5127824732359231213
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8106328193517419873}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ed4e03c2301e55647a46a17478fe384a, type: 3}
m_Name:
m_EditorClassIdentifier:
_destructableSettings: {fileID: 11400000, guid: 5558f512b44c7d74abf20baaba6dc9ef, type: 2}
--- !u!114 &1580681325717503087
MonoBehaviour:
m_ObjectHideFlags: 0
......@@ -167,6 +154,20 @@ MonoBehaviour:
AutoObjectParentSync: 1
SyncOwnerTransformWhenParented: 1
AllowOwnerToParent: 0
--- !u!114 &5127824732359231213
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8106328193517419873}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: ed4e03c2301e55647a46a17478fe384a, type: 3}
m_Name:
m_EditorClassIdentifier:
ShowTopMostFoldoutHeaderGroup: 1
_destructableSettings: {fileID: 11400000, guid: 5558f512b44c7d74abf20baaba6dc9ef, type: 2}
--- !u!4 &8877903449436891099 stripped
Transform:
m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: e13481dedf01cb048b93d9e493c354b4, type: 3}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment