Skip to content

Commit 06cdd18

Browse files
arrows working with layer change
1 parent 23766a3 commit 06cdd18

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

Assets/BossRoom/Scripts/Server/Game/Entity/ServerProjectileLogic.cs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,42 @@
11
using System.Collections.Generic;
2-
using System.IO;
32
using Unity.Netcode;
43
using UnityEngine;
5-
using BossRoom.Scripts.Shared.Net.NetworkObjectPool;
64

75
namespace Unity.Multiplayer.Samples.BossRoom.Server
86
{
9-
107
public class ServerProjectileLogic : NetworkBehaviour
118
{
12-
private bool m_Started = false;
9+
bool m_Started;
1310

1411
[SerializeField]
15-
private NetworkProjectileState m_NetState;
12+
NetworkProjectileState m_NetState;
1613

1714
[SerializeField]
18-
private SphereCollider m_OurCollider;
15+
SphereCollider m_OurCollider;
1916

2017
/// <summary>
2118
/// The character that created us. Can be 0 to signal that we were created generically by the server.
2219
/// </summary>
23-
private ulong m_SpawnerId;
20+
ulong m_SpawnerId;
2421

2522
/// <summary>
2623
/// The data for our projectile. Indicates speed, damage, etc.
2724
/// </summary>
28-
private ActionDescription.ProjectileInfo m_ProjectileInfo;
25+
ActionDescription.ProjectileInfo m_ProjectileInfo;
2926

30-
private const int k_MaxCollisions = 4;
31-
private const float k_WallLingerSec = 2f; //time in seconds that arrows linger after hitting a target.
32-
private const float k_EnemyLingerSec = 0.2f; //time after hitting an enemy that we persist.
33-
private Collider[] m_CollisionCache = new Collider[k_MaxCollisions];
27+
const int k_MaxCollisions = 4;
28+
const float k_WallLingerSec = 2f; //time in seconds that arrows linger after hitting a target.
29+
const float k_EnemyLingerSec = 0.2f; //time after hitting an enemy that we persist.
30+
Collider[] m_CollisionCache = new Collider[k_MaxCollisions];
3431

3532
/// <summary>
3633
/// Time when we should destroy this arrow, in Time.time seconds.
3734
/// </summary>
38-
private float m_DestroyAtSec;
35+
float m_DestroyAtSec;
3936

40-
private int m_CollisionMask; //mask containing everything we test for while moving
41-
private int m_BlockerMask; //physics mask for things that block the arrow's flight.
42-
private int m_NPCLayer;
37+
int m_CollisionMask; //mask containing everything we test for while moving
38+
int m_BlockerMask; //physics mask for things that block the arrow's flight.
39+
int m_NPCLayer;
4340

4441
/// <summary>
4542
/// List of everyone we've hit and dealt damage to.
@@ -49,12 +46,12 @@ public class ServerProjectileLogic : NetworkBehaviour
4946
/// But that's fine by us! We use <c>m_HitTargets.Count</c> to tell us how many total enemies we've hit,
5047
/// so those nulls still count as hits.
5148
/// </remarks>
52-
private List<GameObject> m_HitTargets = new List<GameObject>();
49+
List<GameObject> m_HitTargets = new List<GameObject>();
5350

5451
/// <summary>
5552
/// Are we done moving?
5653
/// </summary>
57-
private bool m_IsDead;
54+
bool m_IsDead;
5855

5956
/// <summary>
6057
/// Set everything up based on provided projectile information.
@@ -80,12 +77,12 @@ public override void OnNetworkSpawn()
8077

8178
m_DestroyAtSec = Time.fixedTime + (m_ProjectileInfo.Range / m_ProjectileInfo.Speed_m_s);
8279

83-
m_CollisionMask = LayerMask.GetMask(new[] { "NPCs", "Default", "Ground" });
84-
m_BlockerMask = LayerMask.GetMask(new[] { "Default", "Ground" });
80+
m_CollisionMask = LayerMask.GetMask(new[] { "NPCs", "Default", "Environment" });
81+
m_BlockerMask = LayerMask.GetMask(new[] { "Default", "Environment" });
8582
m_NPCLayer = LayerMask.NameToLayer("NPCs");
8683
}
8784

88-
private void FixedUpdate()
85+
void FixedUpdate()
8986
{
9087
if (!m_Started) { return; } //don't do anything before OnNetworkSpawn has run.
9188

@@ -105,13 +102,13 @@ private void FixedUpdate()
105102
}
106103
}
107104

108-
private void DetectCollisions()
105+
void DetectCollisions()
109106
{
110-
Vector3 position = transform.localToWorldMatrix.MultiplyPoint(m_OurCollider.center);
111-
int numCollisions = Physics.OverlapSphereNonAlloc(position, m_OurCollider.radius, m_CollisionCache, m_CollisionMask);
107+
var position = transform.localToWorldMatrix.MultiplyPoint(m_OurCollider.center);
108+
var numCollisions = Physics.OverlapSphereNonAlloc(position, m_OurCollider.radius, m_CollisionCache, m_CollisionMask);
112109
for (int i = 0; i < numCollisions; i++)
113110
{
114-
int layerTest = 1 << m_CollisionCache[i].gameObject.layer;
111+
var layerTest = 1 << m_CollisionCache[i].gameObject.layer;
115112
if ((layerTest & m_BlockerMask) != 0)
116113
{
117114
//hit a wall; leave it for a couple of seconds.
@@ -139,15 +136,16 @@ private void DetectCollisions()
139136
m_NetState.RecvHitEnemyClientRPC(targetNetObj.NetworkObjectId);
140137

141138
//retrieve the person that created us, if he's still around.
142-
NetworkObject spawnerNet;
143-
NetworkManager.Singleton.SpawnManager.SpawnedObjects.TryGetValue(m_SpawnerId, out spawnerNet);
144-
ServerCharacter spawnerObj = spawnerNet != null ? spawnerNet.GetComponent<ServerCharacter>() : null;
139+
NetworkManager.Singleton.SpawnManager.SpawnedObjects.TryGetValue(m_SpawnerId, out var spawnerNet);
140+
var spawnerObj = spawnerNet != null ? spawnerNet.GetComponent<ServerCharacter>() : null;
145141

146142
m_CollisionCache[i].GetComponent<IDamageable>().ReceiveHP(spawnerObj, -m_ProjectileInfo.Damage);
147143
}
148144

149145
if (m_IsDead)
146+
{
150147
return; // don't keep examining collisions since we can't damage anybody else
148+
}
151149
}
152150
}
153151
}

0 commit comments

Comments
 (0)