So i have been having problems with syncing a value on my client.
I have already been using Syncvar in health tracking which is working just fine, but the ammo does not?
The difference is the [command] i use in ammo, since only the local player wants to fire, then sends a command to the server that checks if he has the ammo for it, then creates the bullet.
Yet the Client does not recieve any information on his ammunition, it stays the same.
Here are some of the code within the “Planecontroller”:
[SyncVar]
public string playerName;
public Material mat;
public AudioSource gunsound;
public bool shooting;
[SyncVar(hook = "OnAmmoChange")]
public int ammo;
public int overheat = 0;
[SyncVar]
public bool jammed = false;
[SyncVar]
public bool reloading = false;
public AudioSource damsound;
public AudioSource damsound2;
private float speed = 0.08f;
private float maxspeed = 0.18f;
private float minspeed = 0.01f;
public int Rockets = 2;
public int attSpeed = 4;
public int ratefire = 0;
public GameObject nm;
private float throttle = 0.5f;
private float oilDegree = 0.75f;
private bool engine_damage = false;
public Transform speedText;
public Transform throttleText;
public Transform ammoText;
public Transform altitudeText;
public Transform oiltempText;
public Image engineSymbol;
// Update is called once per frame
void Update() {
if (transform.rotation.eulerAngles.z < 90 || transform.rotation.eulerAngles.z > 270)
GetComponent<SpriteRenderer>().flipY = false;
else
GetComponent<SpriteRenderer>().flipY = true;
//The Rate of Fire
if (ratefire > 0)
ratefire--;
//The Overheat
if (overheat > 0)
overheat--;
if (isServer)
{
if (overheat <= 0)
jammed = false;
if (reloading == true)
ammo = ammo+1;
}
if (!isLocalPlayer)
{
return;
//if (shooting && !gunsound.isPlaying)
// gunsound.Play();
}
//Overpower engine smoke
if (throttle > 1.0f && enginesmoke.isPlaying == false)
enginesmoke.Play();
else if (throttle <= 1.0f)
enginesmoke.Stop();
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f / (1 + ((speed / 2) * 25));
//speed = speed + Input.GetAxis("Vertical") * Time.deltaTime * 0.01f;
if (Input.GetAxis("Vertical") != 0.0f)
throttle = throttle + Input.GetAxis("Vertical") * Time.deltaTime * 0.35f;
else if (throttle > 1.0f)
throttle = throttle - 0.5f * Time.deltaTime * 0.35f;
var decceleration = 0.0f;
if (transform.rotation.eulerAngles.z < 90 || transform.rotation.eulerAngles.z > 270) {
if (transform.rotation.z > 0)
decceleration = (1.0f + (WrapAngle(transform.eulerAngles.z))*6);
else
decceleration = (1.0f + (WrapAngle(transform.eulerAngles.z))*2);
} else
{
if (transform.rotation.z > 0)
decceleration = (1.0f + (ReverseAngleDeccelaration(WrapAngle(transform.eulerAngles.z),false))*6);
else
decceleration = (1.0f + (ReverseAngleDeccelaration(WrapAngle(transform.eulerAngles.z),true))*2);
}
//Debug.Log("Dec: "+decceleration+" Rot: "+ WrapAngle(transform.eulerAngles.z));
var acceleration = throttle * (maxspeed / speed);
if (Input.GetAxis("Horizontal") > 0)
acceleration = acceleration - 1.0f;
speed = speed + ((acceleration - decceleration))/10000;
if (throttle < 0)
throttle = 0;
if (engine_damage == true && throttle > 0.75f)
throttle = 0.75f;
else
if (throttle > 1.25f)
throttle = 1.25f;
//Overheating Engine
if (throttle>1.0f)
{
oilDegree = oilDegree+((throttle-1.0f)/500);
} else if (oilDegree > 0.65f)
{
float cooling = 0.001f-(throttle/980);
oilDegree = oilDegree - ((oilDegree/10000)+ cooling);
}
//Damaging Engine by Overheat
if (oilDegree > 1.0f)
{
float chance = Random.Range(0.0f,2.0f);
if (oilDegree + chance >= 3.0f)
engine_damage = true;
}
if (speed < minspeed)
speed = minspeed;
if (speed > 0.3f)
speed = 0.3f;
GetComponent<Rigidbody2D>().drag = 1.0f;
GetComponent<Rigidbody2D>().angularDrag = 1;
GetComponent<Rigidbody2D>().transform.Rotate(0, 0, -x);
GetComponent<Rigidbody2D>().transform.Translate(speed, 0, 0);
//transform.Rotate(0, 0, -x);
//transform.Translate(speed, 0, 0);
//GetComponent<Rigidbody2D>().velocity = new Vector2();
if (speed < 0.05f)
{
GetComponent<Rigidbody2D>().gravityScale = 0.8f-speed;
}
else
{
//Does not reset?
GetComponent<Rigidbody2D>().gravityScale = 0.0f;
}
var altitude = transform.position.y+1500;
//The Speed Throttle & Altitude Indicator
var textMesh = speedText.GetComponent<Text>();
textMesh.text = "Speed: "+ (speed*2000).ToString("f0") + " Km/T";
var textMesh2 = throttleText.GetComponent<Text>();
textMesh2.text = "Throttle: " + (throttle * 100).ToString("f0")+ "%";
var textMesh3 = altitudeText.GetComponent<Text>();
textMesh3.text = "Altitude: " + (altitude*2).ToString("f0") + "m";
var textMesh4 = oiltempText.GetComponent<Text>();
textMesh4.text = "OilTemp: " + (oilDegree*100).ToString("f1") + "°";
if (oilDegree > 1.0f)
textMesh4.color = Color.red;
else
textMesh4.color = Color.black;
if (engine_damage == true)
engineSymbol.enabled = true;
else
engineSymbol.enabled = false;
if (Input.GetKey(KeyCode.Space)&&(ammo>0)&&(jammed==false)) //&&(ratefire<=0)
{
//ratefire = attSpeed;
CmdFire(gameObject);
if (!gunsound.isPlaying)
gunsound.Play();
}
else
if (Input.GetKeyDown(KeyCode.R)&& Rockets>0)
{
CmdFireRocket();
Rockets--;
}
else
{
if (!Input.GetKey(KeyCode.Space))
gunsound.Stop();
}
if (Input.GetKeyDown(KeyCode.V))
{
Debug.Log("i clicked");
GameObject[] objects = GameObject.FindGameObjectsWithTag("Player");
Debug.Log(objects.Length);
foreach (GameObject go in objects)
{
DrawLine(transform.position, go.transform.position, Color.red, 3.0f);
}
}
}
bool CalculateWeapon(PlaneController plane)
{
if (!isServer)
return false;
//Checking for firerate
if (plane.ratefire > 0)
return false;
//Checking for ammo
if (plane.ammo <= 0)
{
plane.reloading = true;
return false;
}
//Checking for jamming
var jam_chance = Random.Range(0,800);
jam_chance = jam_chance + plane.overheat;
if (jam_chance > 1000)
{
plane.jammed = true;
return false;
}
return true;
}
// This [Command] code is called on the Client …
// … but it is run on the Server!
[Command]
void CmdFire(GameObject owner)
{
/*
//Checking for firerate
if (owner.GetComponent<PlaneController>().ratefire > 0)
return;
//Checking for ammo
if (owner.GetComponent<PlaneController>().ammo <= 0)
return;
*/
var plane = owner.GetComponent<PlaneController>();
Debug.Log("before cal" + plane.ammo);
if (CalculateWeapon(plane) == false)
return;
Debug.Log("after cal");
//Taking Ammo
plane.ammo--;
//plane.ammo = plane.ammo-25;
//Increasing overheat
plane.overheat = plane.overheat+5;
//Setting cooldown on attack
plane.ratefire = plane.attSpeed;
// Create the Bullet from the Bullet Prefab
var bullet = (GameObject)Instantiate(
bulletPrefab,
bulletSpawn.position,
bulletSpawn.rotation);
//Add owner to the bullet
bullet.GetComponent<Bullet>().owner = owner;
//Add id to the bullet
//bullet.GetComponent<Bullet>().bulletID = bullet_id;
// Random bullet spray
bullet.transform.Rotate(0, 0, Random.Range(-2f, 2f));
// Add velocity to the bullet
bullet.GetComponent<Rigidbody2D>().velocity = bullet.transform.transform.right * 24;
// Destroy the bullet after 2 seconds
Destroy(bullet, 2.0f);
// Spawn the bullet on the Clients
NetworkServer.Spawn(bullet);
//Signals all clients to make gun sound from this player.
RpcLocalEnemyShoots(owner);
}
Notice that the “Rockets” are local tracked, while the ammo should be server tracked, reason for it was that i wanted the server to handle it instead of the Client to avoid cheats.
I can show the health, but i feel that would “spam” the question to much, though its the same as the tutorial one.