diff --git a/Basic/2DSpaceShooter/Assets/Prefabs/Bullet.prefab b/Basic/2DSpaceShooter/Assets/Prefabs/Bullet.prefab index a263cf092..895cabb0a 100644 --- a/Basic/2DSpaceShooter/Assets/Prefabs/Bullet.prefab +++ b/Basic/2DSpaceShooter/Assets/Prefabs/Bullet.prefab @@ -14,8 +14,6 @@ GameObject: - component: {fileID: 5800000} - component: {fileID: 11400004} - component: {fileID: -4113703611147433057} - - component: {fileID: -2419440837048175228} - - component: {fileID: -7548598001898569089} m_Layer: 0 m_Name: Bullet m_TagString: Untagged @@ -33,6 +31,7 @@ Transform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 17.05494, y: -1.1583278, z: 0} m_LocalScale: {x: 0.6, y: 0.6, z: 0.6} + m_ConstrainProportionsScale: 0 m_Children: - {fileID: 6681543778280045604} - {fileID: 2541587719488337929} @@ -51,6 +50,7 @@ SpriteRenderer: m_CastShadows: 0 m_ReceiveShadows: 0 m_DynamicOccludee: 1 + m_StaticShadowCaster: 0 m_MotionVectors: 1 m_LightProbeUsage: 0 m_ReflectionProbeUsage: 0 @@ -157,45 +157,6 @@ MonoBehaviour: AlwaysReplicateAsRoot: 0 DontDestroyWithOwner: 0 AutoObjectParentSync: 0 ---- !u!114 &-2419440837048175228 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 100000} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: e96cb6065543e43c4a752faaa1468eb1, type: 3} - m_Name: - m_EditorClassIdentifier: - SyncPositionX: 1 - SyncPositionY: 1 - SyncPositionZ: 1 - SyncRotAngleX: 1 - SyncRotAngleY: 1 - SyncRotAngleZ: 1 - SyncScaleX: 1 - SyncScaleY: 1 - SyncScaleZ: 1 - PositionThreshold: 0.001 - RotAngleThreshold: 0.01 - ScaleThreshold: 0.01 - InLocalSpace: 0 - Interpolate: 1 - CanCommitToTransform: 0 ---- !u!114 &-7548598001898569089 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 100000} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 80d7c879794dfda4687da0e400131852, type: 3} - m_Name: - m_EditorClassIdentifier: --- !u!1 &1734169577680067239 GameObject: m_ObjectHideFlags: 0 @@ -223,6 +184,7 @@ Transform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1.6666665, y: 1.6666665, z: 1.6666665} + m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 400000} m_RootOrder: 0 @@ -239,6 +201,7 @@ TrailRenderer: m_CastShadows: 0 m_ReceiveShadows: 1 m_DynamicOccludee: 1 + m_StaticShadowCaster: 0 m_MotionVectors: 0 m_LightProbeUsage: 0 m_ReflectionProbeUsage: 0 @@ -361,6 +324,7 @@ Transform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 0.6784176, y: 0.6784176, z: 0.6784176} + m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 400000} m_RootOrder: 2 @@ -376,6 +340,7 @@ SpriteRenderer: m_CastShadows: 0 m_ReceiveShadows: 0 m_DynamicOccludee: 1 + m_StaticShadowCaster: 0 m_MotionVectors: 1 m_LightProbeUsage: 0 m_ReflectionProbeUsage: 0 @@ -443,6 +408,7 @@ Transform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1.2764332, y: 1.2764332, z: 1.2764332} + m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 400000} m_RootOrder: 1 @@ -459,6 +425,7 @@ TrailRenderer: m_CastShadows: 0 m_ReceiveShadows: 1 m_DynamicOccludee: 1 + m_StaticShadowCaster: 0 m_MotionVectors: 0 m_LightProbeUsage: 0 m_ReflectionProbeUsage: 0 diff --git a/Basic/2DSpaceShooter/Assets/Scripts/Bullet.cs b/Basic/2DSpaceShooter/Assets/Scripts/Bullet.cs index a8bb46c14..ad398c6ed 100644 --- a/Basic/2DSpaceShooter/Assets/Scripts/Bullet.cs +++ b/Basic/2DSpaceShooter/Assets/Scripts/Bullet.cs @@ -38,6 +38,26 @@ private void DestroyBullet() NetworkObject.Despawn(true); } + public void SetVelocity(Vector2 velocity) + { + if (IsServer) + { + var bulletRb = GetComponent(); + bulletRb.velocity = velocity; + SetVelocityClientRpc(velocity); + } + } + + [ClientRpc] + void SetVelocityClientRpc(Vector2 velocity) + { + if (!IsHost) + { + var bulletRb = GetComponent(); + bulletRb.velocity = velocity; + } + } + void OnCollisionEnter2D(Collision2D other) { var otherObject = other.gameObject; diff --git a/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs b/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs index 6380417ab..0bc1e338c 100644 --- a/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs +++ b/Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs @@ -196,23 +196,24 @@ void Fire(Vector3 direction) { fireSound.Play(); - int damage = 5; + var damage = 5; if (QuadDamageTimer.Value > NetworkManager.ServerTime.TimeAsFloat) { damage = 20; } - bool bounce = BounceTimer.Value > NetworkManager.ServerTime.TimeAsFloat; + var bounce = BounceTimer.Value > NetworkManager.ServerTime.TimeAsFloat; - GameObject bullet = m_ObjectPool.GetNetworkObject(BulletPrefab).gameObject; - bullet.transform.position = transform.position + direction; + var bulletGo = m_ObjectPool.GetNetworkObject(BulletPrefab).gameObject; + bulletGo.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, m_BulletLifetime); - bullet.GetComponent().Spawn(true); + bulletGo.GetComponent().Spawn(true); + var bullet = bulletGo.GetComponent(); + bullet.Config(this, damage, bounce, m_BulletLifetime); + bullet.SetVelocity(velocity); + } void Update() diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f7b2d8fe..87fceb17a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## [unreleased] - yyyy-mm-dd +### 2DSpaceShooter + +### Fixed +- Fixing bullet explosion desync (#89). Bullet explosion vfx were happening too early on clients because of NetworkTransform's interpolation. Bullets are now no longer synchronised by NetworkTransforms and instead only have their velocity set through client rpcs when they are spawned. Since they are no longer interpolated, they are not lagging behind the server and are at the correct position when they receive the despawn message from the server. + ## [1.1.0] - 2022-12-13 ### Client Driven