Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Superkaj · Jan 24, 2018 at 02:21 PM · valuesyncclient-servercommand

Syncvar does not sync on Client

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Rex240 · Jun 07, 2018 at 04:42 PM

You seem to have a hook attached to the ammo SyncVar but I can't find the function OnAmmoChange but you have not implemented it. If that's the case, then the SynVar will not be updated on the clients. You can add a simple function like so to fix this.

void OnAmmoChange(int newAmmo) { ammo = newAmmo; }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

SyncVar, Command and Authority problem 1 Answer

[Command] on instantiated objects 0 Answers

How to change turns on button click using HLAPI? 0 Answers

Calling Bool in Network Command Not Working 0 Answers

ClientRPC not called from command on client instance during Start() 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges