How do i fix a multiplayer fps health issue?
Im making a multiplayer game and when i host a game i could kill the other player but the other player can't kill the host. I have a health script that works for the host but not the client, but when i turn off the health, and uncomment out this line ("CmdHitPlayer(hit.transform.gameObject);")the other player can now kill the host no problem.
Health Script: using UnityEngine; using UnityEngine.Networking;
public class Health : NetworkBehaviour { //The box's current health point total public const int maxHealth = 100; [SyncVar] public int currentHealth = maxHealth;
public void Damage(int damageAmount)
{
if (!isServer)
{
return;
}
//subtract damage amount when Damage function is called
currentHealth -= damageAmount;
//Check if health has fallen below zero
if (currentHealth <= 0)
{
//Destroy(gameObject);
//if health has fallen below zero, deactivate it
CmdHitPlayer(transform.gameObject);
currentHealth = maxHealth;
}
}
[Command]
void CmdHitPlayer(GameObject hit)
{
hit.GetComponent<NetworkedPlayerScript>().RpcResolveHit();
}
}
Shooting Script: using UnityEngine; using UnityEngine.Networking;
public class ShootingScript1 : NetworkBehaviour { public ParticleSystem _muzzleFlash; public AudioSource _gunAudio; public GameObject _impactPrefab; public Transform cameraTransform;
ParticleSystem _impactEffect;
public int gunDamage = 25;
public float range;
public float fireRate;
void Start()
{
_impactEffect = Instantiate(_impactPrefab).GetComponent<ParticleSystem>();
}
void Update()
{
//if (Input.GetButtonDown("Fire2"))
//CmdHitPlayer(gameObject);
if (Input.GetButtonDown("Fire1"))
{
_muzzleFlash.Stop();
_muzzleFlash.Play();
_gunAudio.Stop();
_gunAudio.Play();
RaycastHit hit;
Vector3 rayPos = cameraTransform.position + (1f * cameraTransform.forward);
if (Physics.Raycast(rayPos, cameraTransform.forward, out hit, range))
{
Health health = hit.transform.GetComponent<Health>();
_impactEffect.transform.position = hit.point;
_impactEffect.Stop();
_impactEffect.Play();
if (hit.transform.tag == "Player")
{
health.Damage(gunDamage);
//CmdHitPlayer(hit.transform.gameObject);
}
}
}
}
/*[Command]
void CmdHitPlayer(GameObject hit)
{
hit.GetComponent<NetworkedPlayerScript>().RpcResolveHit();
}*/
}
You deleted the other post where I answered your question... Not sure why you did that but it doesn't exactly encourage anyone to continue helping.
Sorry but i had that post up for a few days now and no one was helping. I been having the problem for a week now and i cant find a solution. So i deleted it and changed it to put it in the help room.
I gave you the answer already. And you responded that you found your answer. What happened?
Your answer
Follow this Question
Related Questions
Client can damage host but not vice versa? 0 Answers
Help with photon being used to make a mulitplayer fps 0 Answers
[Help!] Showing only your health in a Multiplayer FPS game 0 Answers
Networking Fps Cameras 0 Answers
MultiplayerFPS - database error 1 Answer