Search Unity

Basic RPC

Discussion in 'Multiplayer' started by rmele09, Mar 26, 2015.

  1. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    716
    I am new to networking with unity. I am curious about sending what I think is a basic RPC, in which I want to change a variable (health) on collision. I am prototyping a FPS type experience, for now there is only 2 players, a server and a client. So the example for the RPC can go to "others" I think. Here is my code with no networking, how can I make this into an RPC and have it effect the client?

    Code (JavaScript):
    1. function OnTriggerEnter (other : Collider)
    2. {
    3. if (other.gameObject.tag == "Other_Player")              
    4.     {
    5.     Enemy();
    6.     }
    7.  
    8. }// END OnTriggerEnter
    9. function Enemy () {                
    10.  
    11.     var enemy = GameObject.FindWithTag("PlayerBodyMesh").transform;
    12.     enemy.GetComponent.<Player>().playerHealth -= 20;
    13. }
     
  2. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    After the client is connected to server, i think you can use that

    Code (CSharp):
    1.     function OnTriggerEnter (other : Collider)
    2.     {
    3.     if (other.gameObject.tag == "Other_Player")            
    4.         {
    5.         Enemy();
    6.         }
    7.    
    8.     }// END OnTriggerEnter
    9.     function Enemy () {              
    10.    
    11.         var enemy = GameObject.FindWithTag("PlayerBodyMesh").transform;
    12.      
    13.         float damage = -20;
    14.         GetComponent<NetworkView>().RPC("DamageReceived", RPCMode.OthersBuffered, damage);
    15.     }
    16.  
    17. [RPC]
    18. void DamageReceived(float damage)
    19. {
    20.   enemy.GetComponent.<Player>().playerHealth += damage;
    21. }
    i think this is what you're looking for...
     
    Last edited: Mar 27, 2015
  3. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    716
    Thanks I'll give this a go!