Skip to content

Commit

Permalink
Player shots will not hit the player now.
Browse files Browse the repository at this point in the history
  • Loading branch information
plaidpants committed Feb 3, 2022
1 parent 1cb5a4a commit 989df42
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 2 deletions.
Binary file modified Assets/Game1.unity
Binary file not shown.
18 changes: 16 additions & 2 deletions Assets/RocketSphere.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class RocketSphere : NetworkBehaviour
public GameObject explosionPrefab;
Rigidbody rb;
[SyncVar] Color RocketColor = Color.white;
float hue = 2.0f;
public float hue = 2.0f;
[SyncVar] bool visible = false;
[SyncVar] Quaternion rot2Save = Quaternion.identity;

Expand Down Expand Up @@ -292,6 +292,17 @@ void OnTriggerEnter(Collider other)
// Rocket is already destroyed, ignore
if (!rocket.activeSelf) return;

ShotSphere shot = other.attachedRigidbody.GetComponent<ShotSphere>();

if (shot)
{
if (shot.playershooterhue == hue)
{
// ignore our own shots
return;
}
}

// Spawn an explosion at player position on all clients
GameObject explosion = Instantiate(explosionPrefab, rocket.transform.position, Quaternion.identity) as GameObject;
NetworkServer.Spawn(explosion);
Expand All @@ -315,8 +326,11 @@ void Fire()

GameObject shot = Instantiate(shotPrefab, pos, rot) as GameObject;

shot.transform.rotation = transform.rotation;
// save the hue of the shooter in the shot so we won't collide with it later
shot.GetComponent<ShotSphere>().playershooterhue = hue;

shot.transform.rotation = transform.rotation;

Rigidbody rbshot = shot.GetComponent<Rigidbody>();
rbshot.angularVelocity = rb.angularVelocity;

Expand Down
12 changes: 12 additions & 0 deletions Assets/ShotSphere.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class ShotSphere : NetworkBehaviour
public GameObject shotPrefab;
GameObject shot;
public GameObject explosionPrefab;
public float playershooterhue = -10.0f;
bool destroyed = false;

public override void OnStartServer()
Expand Down Expand Up @@ -42,6 +43,17 @@ void Start()
[ServerCallback]
void OnTriggerEnter(Collider other)
{
RocketSphere rocket = other.attachedRigidbody.GetComponent<RocketSphere>();

if (rocket)
{
if (rocket.hue == playershooterhue)
{
// ignore our own shots
return;
}
}

if (destroyed == false)
{
destroyed = true;
Expand Down
Binary file modified Assets/ShotSphere.prefab
Binary file not shown.
Binary file modified ProjectSettings/EditorBuildSettings.asset
Binary file not shown.
Binary file modified ProjectSettings/GraphicsSettings.asset
Binary file not shown.
Binary file modified ProjectSettings/ProjectSettings.asset
Binary file not shown.

0 comments on commit 989df42

Please sign in to comment.