Question by
ZurielB · Jan 03, 2018 at 06:01 AM ·
networkingfpsserverdamage
[FPS Multipler] Host only one that can kill.
Not sure if my post got posted. If it didn't here question. I am working on an Multiplayer fps and having some problems. I am currently using the Unity server, and having the problem of the host been the only one to kill a player. I having been looking at tutorials, and It's the first time I working with the multiplayer area. Here is my health code. I don't know if its my code or am I missing something.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Health : NetworkBehaviour {
[SerializeField]
public const int maxHealth = 100; //Heath of Iteam
[SyncVar]
public int currentHealth = maxHealth;
private NetworkStartPosition[] spawnPoints;
void Start (){
if (isLocalPlayer){
spawnPoints = FindObjectsOfType<NetworkStartPosition>();
}
}
public void TakeDamage (int amount){
if(!isServer){
return;
}
//Subtracts the amount(damage) from health
currentHealth -= amount;
//Checks if health is less than 0 to kill object
if(currentHealth <= 0){
currentHealth = maxHealth;
RpcRespawn();
}
}
[ClientRpc]
void RpcRespawn(){
if (isLocalPlayer){
// Set the spawn point to origin as a default value
Vector3 spawnPoint = Vector3.zero;
// If there is a spawn point array and the array is not empty, pick one at random
if (spawnPoints != null && spawnPoints.Length > 0)
{
spawnPoint = spawnPoints[Random.Range(0, spawnPoints.Length)].transform.position;
}
// Set the player’s position to the chosen spawn point
transform.position = spawnPoint;
}
}
}
This is my Gun Script.
using UnityEngine;
public class Gun : MonoBehaviour {
public float damage = 25f; //Damage for gun
public int damagePlayer = 10;
public float range = 100f; //Range for gun
public float forceAdded;
public ParticleSystem muzzleFlash; //Particle for the muzzle flash
public Camera fpsCam; //Camera
// Update is called once per frame
void Update () {
if(Input.GetButtonDown("Fire1")){
Shoot();
}
}
void Shoot (){
RaycastHit hit;
if(Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range)){
//Plays the Muzzle Flash
muzzleFlash.Play();
//Debug.Log(hit.transform.name);
Health health = hit.transform.GetComponent<Health>();
//Checks if anything was hit
if (health != null){
//Calls the Target script to add damage to the oject hitted.
health.TakeDamage(damagePlayer);
}
//Get whatever was hit
Target target = hit.transform.GetComponent<Target>();
//Checks if anything was hit
if (target != null){
//Calls the Target script to add damage to the oject hitted.
target.TakeDamage(damage);
}
//This give a force to anything the bullet hits
if (hit.rigidbody != null){
hit.rigidbody.AddForce(-hit.normal * forceAdded);
}
}
}
}
This is a link to download version to my game. If it helps. https://gamejolt.com/games/ai/308730 I have been mainly following the Unity Basic Multiplayer Tutorial. Thanks
Comment
Your answer
