Multiplayer FPS help

Updated on April 24, 2017 in [A] Brackeys Courses
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on April 17, 2017

In episode 6 of the fps tutorial, at the end, we get to where we will have the console print that a player has been shot. For some reason though, it isnt. It should work, because it says that it knows there are two players with different names, so the ID system is for sure working. Does someone have their code I can see to see whats up?

  • Liked by
  • CeriThomas
Reply
0 on April 24, 2017

Here my PlayerShoot script 

using UnityEngine;
using UnityEngine.Networking;
public class PlayerShoot : NetworkBehaviour {
 private const string PLAYER_TAG = "Player";
 public PlayerWeapon weapon;
 [SerializeField]
 private Camera cam;
 [SerializeField]
 private LayerMask mask;
 void Start()
 {
 if (cam == null)
 {
 Debug.LogError("PlayerShoot: No camera referenced");
 this.enabled = false;
 }
 }
 void Update()
 {
 if (Input.GetButtonDown("Fire1"))
 {
 Shoot();
 }
 }
 [Client]
 void Shoot()
 {
 RaycastHit _hit;
 if (Physics.Raycast(cam.transform.position, cam. transform.forward, out _hit, weapon.range, mask) )
 {
 if(_hit.collider.tag == PLAYER_TAG)
 {
 CmdPlayerShot(_hit.collider.name, weapon.damage);
 }
 }
 }
 [Command]
 void CmdPlayerShot(string _playerID, int _damage)
 {
 Debug.Log(_playerID + " has been shot.");
 Player _player = GameManager.GetPlayer(_playerID);
 _player.TakeDamage();
 }
}

and my GameManager script

using UnityEngine;
using System.Collections.Generic;
public class GameManager : MonoBehaviour {
 private const string PLAYER_ID_PREFIX = "Player ";
 private static Dictionary<string, Player> players = new Dictionary<string, Player>();
 public static void RegisterPlayer(string _netID, Player _player)
 {
 string _playerID = PLAYER_ID_PREFIX + _netID;
 players.Add(_playerID, _player);
 _player.transform.name = _playerID;
 }
 public static void UnRegisterPlayer (string _playerID)
 {
 players.Remove(_playerID);
 }
 public static Player GetPlayer (string _playerID)
 {
 return players[_playerID];
 }
 //void OnGUI()
 //{
 // GUILayout.BeginArea(new Rect(200, 200, 200, 500));
 // GUILayout.BeginVertical();
 // foreach (string _playerID in players.Keys)
 // {
 // GUILayout.Label(_playerID + " - " + players[_playerID].transform.name);
 // }
 // GUILayout.EndVertical();
 // GUILayout.EndArea();
 }

 

  • Liked by
Reply
Cancel