From 96dcaa6e9a78f7577fb732bbe021f96029a33f32 Mon Sep 17 00:00:00 2001 From: Luke Stampfli Date: Mon, 16 Aug 2021 07:54:17 +0100 Subject: [PATCH] fix: Fix a bug where the hit friction vfx would only be played on the server --- .../Assets/Scripts/ShipControl.cs | 50 +++++++++++++------ 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs b/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs index 6d3c55302..38a75b93e 100644 --- a/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs +++ b/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs @@ -31,7 +31,7 @@ public static Color GetColor(BuffType bt) public class ShipControl : NetworkBehaviour { static string s_ObjectPoolTag = "ObjectPool"; - + NetworkObjectPool m_ObjectPool; public GameObject BulletPrefab; public AudioSource fireSound; @@ -62,7 +62,7 @@ public class ShipControl : NetworkBehaviour public ParticleSystem friction; public ParticleSystem thrust; - float m_FrictionStopTimer = 0; + private NetworkVariableFloat m_FrictionEffectStartTimer = new NetworkVariableFloat(-10); // for client movement command throttling float m_OldMoveForce = 0; @@ -70,9 +70,9 @@ public class ShipControl : NetworkBehaviour // server movement private NetworkVariableFloat m_Thrusting = new NetworkVariableFloat(); - + float m_Spin; - + Rigidbody2D m_Rigidbody2D; void Awake() @@ -99,7 +99,6 @@ void OnDisable() void Start() { - friction.Stop(); thrust.Stop(); DontDestroyOnLoad(gameObject); @@ -114,8 +113,7 @@ public override void OnNetworkSpawn() public void TakeDamage(int amount) { Health.Value = Health.Value - amount; - friction.Play(); - m_FrictionStopTimer = Time.time + 1.0f; + m_FrictionEffectStartTimer.Value = NetworkManager.LocalTime.TimeAsFloat; if (Health.Value <= 0) { @@ -142,20 +140,20 @@ void Fire(Vector3 direction) } bool bounce = BounceTimer.Value > Time.time; - + GameObject bullet = m_ObjectPool.GetNetworkObject(BulletPrefab).gameObject; bullet.transform.position = transform.position + direction; - + var bulletRb = bullet.GetComponent(); var velocity = m_Rigidbody2D.velocity; velocity += (Vector2)(direction) * 10; bulletRb.velocity = velocity; bullet.GetComponent().Config(this, damage, bounce, bulletLifetime); - + bullet.GetComponent().Spawn(null, true); } - + void Update() { if (IsServer) @@ -224,16 +222,36 @@ void UpdateServer() } } - void UpdateClient() + private void HandleFrictionGraphics() { - if (!IsLocalPlayer) + var time = NetworkManager.ServerTime.Time; + var start = m_FrictionEffectStartTimer.Value; + + bool frictionShouldBeActive = time >= start && time < start + 1f; // 1f is the duration of the effect + + if (frictionShouldBeActive) { - return; + if (friction.isPlaying == false) + { + friction.Play(); + } + } + else + { + if (friction.isPlaying) + { + friction.Stop(); + } } + } - if (m_FrictionStopTimer < Time.time) + void UpdateClient() + { + HandleFrictionGraphics(); + + if (!IsLocalPlayer) { - friction.Stop(); + return; } // movement