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 Punkjim420 · Jan 28, 2014 at 04:54 AM · c#rpconlinesynchealth

c#, RPC, Networking, Multiplayer Variables need 2 sync properly.

I have a few scripts that i have made using tutorials and such. They work together to create an online game with blocks as each player and a working chat, the blocks need health, i created a variable inside the playerscript.cs and placed it on the blocks so that they each have 100 hp, i created a button that when pressed the health is increased by one. but the health does not sync properly. if player 1 adds 1 hp, player 2 gains 1 hp also, and on the clients of others nobody got any hp not even player 1. i used rpc to track the playerHp variable but its not working, i think rpc is for giving a message or setting new values rather than tracking and comparing existing ones.. so im not sure how to do this and wondered if anyone could offer some advice.

problem: if i press button, player1 hp= playerhp+1 AND player2 hp = player2 hp+1 question: whats a good method to update health values and tell the other clients without changing their health values? i just want to "read" what my friends health values are, and see if they have levelled up achieved bigger health values. sorry for the long post, thanks for reading it.

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour
 {
     public float speed = 10f;
     public float playerHp = 100;
     private float lastSynchronizationTime = 0f;
     private float syncDelay = 0f;
     private float syncTime = 0f;
     private Vector3 syncStartPosition = Vector3.zero;
     private Vector3 syncEndPosition = Vector3.zero;
     private Quaternion syncStartRotation = Quaternion.identity;
     private Quaternion syncEndRotation = Quaternion.identity;
     private GameObject Target = null;
     void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
     {
         Vector3 syncPosition = Vector3.zero;
         Vector3 syncVelocity = Vector3.zero;
         float syncPlayerHp = 100;
         Quaternion syncRotation = Quaternion.identity;
         if (stream.isWriting)
         {
             syncPosition = rigidbody.position;
             stream.Serialize(ref syncPosition);
 
             syncPosition = rigidbody.velocity;
             stream.Serialize(ref syncVelocity);
 
             syncRotation = rigidbody.rotation;
             stream.Serialize(ref syncRotation);
 
             syncPlayerHp = playerHp;
             stream.Serialize (ref syncPlayerHp);
         }
         else
         {
             stream.Serialize(ref syncPosition);
             stream.Serialize(ref syncVelocity);
             stream.Serialize(ref syncRotation);
             stream.Serialize (ref syncPlayerHp);
 
             syncTime = 0f;
             syncDelay = Time.time - lastSynchronizationTime;
             lastSynchronizationTime = Time.time;
 
             syncEndPosition = syncPosition + syncVelocity * syncDelay;
             syncStartPosition = rigidbody.position;
             syncEndRotation = syncRotation;
             syncStartRotation = rigidbody.rotation;
         }
     }
 
     void Awake()
     {
         lastSynchronizationTime = Time.time;
     }
 
     void Update()
     {
         if (networkView.isMine)
         {
             InputMovement();
             InputColorChange();
         }
         else
         {
             SyncedMovement();
         }
         if (Input.GetKeyDown(KeyCode.F)){
             this.playerHp = playerHp + 1;
         }
 
     }
 
 
     private void InputMovement()
     {
         if (Input.GetKey (KeyCode.W))
             transform.Translate (Vector3.forward * 5 * Time.deltaTime);
                 
         if (Input.GetKey(KeyCode.S))
             transform.Translate (Vector3.back * 5 * Time.deltaTime);
 
         if (Input.GetKey(KeyCode.E))
             transform.Translate (Vector3.right * 5 * Time.deltaTime);
 
         if (Input.GetKey(KeyCode.Q))
             transform.Translate (Vector3.left * 5 * Time.deltaTime);
 
         if (Input.GetKey(KeyCode.A))
             transform.Rotate (Vector3.up * -80 * Time.deltaTime);
 
         if (Input.GetKey(KeyCode.D))
             transform.Rotate (Vector3.up * 80 * Time.deltaTime);
 
         if (Input.GetKey (KeyCode.Space))
             rigidbody.AddForce(Vector3.up * 10);
     }
 
     private void SyncedMovement()
     {
         syncTime += Time.deltaTime;
 
         rigidbody.position = Vector3.Lerp(syncStartPosition, syncEndPosition, syncTime / syncDelay);
         rigidbody.rotation = Quaternion.Slerp(syncStartRotation, syncEndRotation, syncTime / syncDelay);
     }
 
 
     private void InputColorChange()
     {
         if (Input.GetKeyDown(KeyCode.R))
             ChangeColorTo(new Vector3(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f)));
     }
 
     [RPC] void ChangeColorTo(Vector3 color)
     {
         renderer.material.color = new Color(color.x, color.y, color.z, 1f);
 
         if (networkView.isMine)
             networkView.RPC("ChangeColorTo", RPCMode.OthersBuffered, color);
     }
     void OnGUI(){
         GUI.Box (new Rect (Screen.width - 100,Screen.height - 20, 100, 20), "Hp: " + playerHp);
         if(Target != null){
             GUI.Box (new Rect(Screen.width/2 - 50, 0, 300, 20), Target.name + " Hp: " + playerHp);
         }
     }
     void OnMouseEnter(){
         Target = this.gameObject;
     }
     void OnMouseExit(){
         Target = null;
     }
 }

 
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
Best Answer

Answer by Punkjim420 · Jan 28, 2014 at 06:01 AM

i found the problem, it was the way i added hp and i missed something in my else statement at around line 36 i needed:

 playerHp = syncPlayerHp;
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

18 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

Related Questions

[Error] RPC function undefined in IRRELEVANT file? 0 Answers

Multiple Cars not working 1 Answer

hi i have a problem with sync my particle system with mirror to the other players(only host gets the system wotking) 0 Answers

Distribute terrain in zones 3 Answers

RPC replace the existing list on all players 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