Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by terjesorheim · Dec 27, 2013 at 11:37 AM · networkvariablehealth

Trouble syncing health in network

Hi I am trying to create a network game. I have some trouble syncing the health of structures etc - newly logged on users don't see the same as the server and other currently online players. Below is an example. The server player logged in and did some damage to the cubes. Then the Player 1 logg in and the cubes health is set to the default value, and NOT to the current health. BUT if either of the players do more damage, both see the same (and correct number).

alt text

I attaches this script to the cubes, and configure the amount of default health in the inspector.

 #pragma strict
 
 var HP:float;
 private var localHP:float;
 
 var explosionPrefab:GameObject;
 private var prefabPosition:Transform;
 
 
 function Start () {
 //Debug.Log("HP "+ HP);
 //Debug.Log("LHP " + localHP);    
 }
 
 
 function takeDamage(damageVal:float){
     Debug.Log(gameObject.name + " is taking "+ damageVal +" damage!");
     HP -= damageVal;
     if(HP <= 0){
         Network.Destroy(gameObject);
         Instantiate(explosionPrefab, transform.position, Quaternion.identity);
     }
     
     if(networkView.isMine) {        
         networkView.RPC("sendHPToServer", RPCMode.AllBuffered, HP, transform.position);
     } 
 }
 
 
 private var namePlatePos : Vector3;
 var namePlate:GUIStyle;
 var showHP:boolean;
 
 function OnGUI(){
     // Place the name plate where the gameObject is    
     if(showHP){
         namePlatePos = Camera.main.WorldToScreenPoint(gameObject.transform.position);    
         GUI.BeginGroup(Rect((namePlatePos.x-50), (Screen.height - namePlatePos.y), 100, 50));
             GUI.Label(Rect(0, 0, 100, 50), HP + "%",namePlate);        
         GUI.EndGroup();
     }    
 }
     
 function OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo) {
     if (stream.isWriting) {
         var healthC : float = HP;
         stream.Serialize(healthC);        
     } else {
         var healthZ : float = 0;
         stream.Serialize(healthZ);
         HP = healthZ;        
     }    
 }
 
 @RPC
 function sendHPToServer(newHP:float, location:Vector3) {
     HP = newHP;
     //transform.position = location;
 }


How can I make shure newly logged on players see the current damage done to the cubes. I use on Network View to track the transforms of the cube. Can I use one to track variables?

I use this code on the Grenade object:

 function explode() {
     // Applies an explosion force to all nearby rigidbodies
     var explosionPos : Vector3 = transform.position;
     var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
     
     for (var hit : Collider in colliders) {
     if (hit && hit.rigidbody)
         hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0);
         hit.SendMessage("takeDamage",damage);
     }
     
     Destroy(gameObject);
     Instantiate(explosionPrefab, transform.position, Quaternion.identity);
 }

Please help me with this :)

unity-variablesyncproblem.jpg (205.1 kB)
Comment
Add comment · Show 2
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
avatar image nastasache · Dec 27, 2013 at 12:21 PM 0
Share

Just an opinion, I am not sure, but I think you don't need to use both RPC and serialization for HP. I think, calling sendHPToServer() on all buffered, as you did, it's enough to update and show the new HP value on GUI for all already connected and new connected players.

avatar image ShadoX · Dec 27, 2013 at 01:00 PM 0
Share

Don't know much about Unitys networking, but I only see a RPC call when the Cube takes damage, so it makes sense why it's updated for players when the Cube get's hit.

Thing is, where is the code that would actually send the HP info the player that just joined the server ? Not sure how your game is set up, but usually you'd send all that info to the Client / Player when he joins and when the info is of any value to the client.

(I'm just guessing since I have no idea how those Unity servers work and if they should send this kind of info automatically to the client or if you're supposed to program that part..)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by terjesorheim · Dec 30, 2013 at 07:35 PM

I removed the OnSerializeNetworkView(), but still the correct values don't show for the new players.

I was thinking the same thing, ShadoX. But I can't figure out what code to run at startup that sets the HP to the buffered value. I tought Unity it self buffered and remembered network objects.

I updated the players name plates, that works just fine.

Need more tips? Has anyone tried this before?

Next up is to track each players HP, so I have to make this work


UPDATED: It appears that the code I use is correct. The trouble was that my network lobby lost some variables (and probably other stuff) when I loaded the level. The lobby and the level is now in the same scene, and everything work as intended!**

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

20 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

Related Questions

Load variable from server 0 Answers

How to do health thru network 1 Answer

Strings through a network? 1 Answer

Specific enemy variable affects all enemies 2 Answers

Instantiate health bar with networking 2 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