- Home /
How does the client command the server in Javascript?
My friend and I are working on an online FPS game, and right now we can't solve this problem. We want to have it so that when the client player hits the zombie, it commands the server to decrease the health of the zombie by one, because the way it is right now it only decreases the health for the client, and when the client kills the zombie it doesn't die for the server. We are very inexperienced so sorry if we are missing something obvious. Please help!
Answer by PieSorcerer · Sep 05, 2015 at 12:17 PM
I think I pretty much solved the problem. For anyone else with my troubles, this is the script we're using now: (File Name: JSPlayer_Shoot)
import UnityEngine.Networking;
class JSPlayer_Shoot extends NetworkBehaviour {
private var damage : int = 25;
private var range : float = 200;
var camTransform : Transform ;
private var hit : RaycastHit;
function Start () {
}
function Update ()
{
CheckIfShooting();
}
function CheckIfShooting()
{
if(!isLocalPlayer)
{
return;
}
if(Input.GetKeyDown(KeyCode.Mouse0))
{
Shoot();
}
}
function Shoot()
{
if(Physics.Raycast(camTransform.TransformPoint(0, 0, 0.5f), camTransform.forward, hit, range))
{
if(hit.transform.tag == "Zombie")
{
var uIdentity : String = hit.transform.name;
CmdTellServerWhichZombieWasShot(uIdentity, damage);
}
}
}
@Command
function CmdTellServerWhichZombieWasShot (uniqueID : String, dmg : int)
{
var go : GameObject = GameObject.Find(uniqueID);
go.GetComponent(ZombieHP).hp --;
}
}
Your answer
Follow this Question
Related Questions
Distance on fps 3 Answers
Objects not showing in a network game 2 Answers
How can I get the ping from the master server? 1 Answer
Lens-Flare Muzzleflash 0 Answers
Enemy death help 1 Answer