Search Unity

[Help]Show changes in mesh to other users

Discussion in 'Multiplayer' started by unitygirl, Jun 4, 2010.

  1. unitygirl

    unitygirl

    Joined:
    May 25, 2010
    Posts:
    38
    Hi, I'm currently making some multiplayer game sample using M2H Network Tutorial, and currently I'm stuck. :(

    In this game, I'm using mouse click to move the character and this is done by raycasting.

    When my character clicks the cube, the cube's isTrigger is set to true, which makes cube into trigger. (so character can actually move to the cube's position without colliding) Then as my character enters the cube(which has become a trigger), it instantiates the sphere with isTrigger turned on(using Instantiate) and simulatenously makes my character and cube invisible (this is done by turning mesh renderer false). Then, if I click outside this sphere (so i'm clicking the plane where character is standing on), it makes my character and cube visible again, cube's isTrigger becomes false and destroys sphere.

    This process is well shown to the user who is achieving this, but this process cannot be shown to the other user who is connected and watching this process, like picture below:

    So the user who made the server has clicked the cube, and instantiated the sphere and successfully made the character itself and the cube invisible.
    http://cfile204.uf.daum.net/original/194A77264C08A1187F97FF

    But on client side, Client user is watching the Server user and as picture shows below, it is just running towards the cube, not doing the effects that is being done on server side.
    http://cfile224.uf.daum.net/original/184A77264C08A1187EBDA0

    This is code attatched to the cube.
    Code (csharp):
    1.  
    2.  
    3. public var sitModelPrefab: Transform;
    4. public var sitModel;
    5. public var Entered: boolean = false;
    6.  
    7.  
    8. function OnTriggerEnter (other : Collider){    
    9.     //Check to see if a player entered the gate, rather than some space debris.    
    10.     if (other.gameObject.CompareTag("Player"))
    11.         {        
    12.             // Make player and cube invisible
    13.            
    14.             other.gameObject.GetComponent(MeshRenderer).enabled = false;
    15.             gameObject.GetComponent(MeshRenderer).enabled = false;
    16.            
    17.             // Instantiate replacement sitting character model
    18.             sitModel = Instantiate(sitModelPrefab, transform.position, transform.rotation);
    19.             sitModel.collider.isTrigger = true;
    20.            
    21.            
    22.             //CopyTransformsRecurse(transform, sitModel);
    23.         }  
    24. }
    25.  
    26. function OnCollisionExit (other: Collision)
    27. {
    28.     Debug.Log("Exiting Chair");
    29.     other.gameObject.GetComponent(MeshRenderer).enabled = true;
    30.     gameObject.GetComponent(MeshRenderer).enabled = true;
    31.     if(sitModel != null)
    32.     {
    33.         Destroy(sitModel.gameObject);
    34.     }
    35.    
    36. }
    37. function Start()
    38. {
    39.     collider.isTrigger = false;
    40. }
    41.  
    42.  
    43.  
    44. function OnMouseDown()
    45. {
    46.     collider.isTrigger = true;
    47. }
    48.  
    49. function Update()
    50. {
    51.    
    52.     //Debug.Log("EnterPad is Trigger: " + collider.isTrigger);
    53. }
    54.  
    55.  
    Thank you for reading such a long story. Reason I've done really complex way of turning isTrigger on and off is because later on, I'm going to make this cube into chair and when my character touches it, character becomes invisible and replacement object which has appearance of character sitting on chair will be there. Is there any heroes who can help me with this?
     
  2. zelk

    zelk

    Joined:
    Mar 13, 2009
    Posts:
    246
    That was a long story but you did get to the point. :)

    The thing with networking is that you must think a bit differently. As you might have realized, User input on a client should not do anything else than sending messages to all players, using RPC or maybe network view sync. The same goes with things like this. I would implement it like this:

    If/when you realize that your player collided with something and that something should happen, just run an RPC command to everybody saying what happened. That means that the RPC-function will be run on all computers, including your own. In the RPC-function, take action.
     
  3. zelk

    zelk

    Joined:
    Mar 13, 2009
    Posts:
    246
    To clearify, here is what I did in my game (pseudo code) - not using authoritive server:

    if user pressed shoot button I use a ray to decide what got hit. Then, I run an RPC function on all computers telling about where the shot was fired, where it hit and what player got hit.

    In SHOOT on all computers, they visualize the shot and the hit. Each of them check if they are the one that got it. The one that got hit, substracts health (and tells the other computers about it using another RPC). If the players health was zero I run RPC command PlayerKilled on all computers so that everybody knows about it.

    When doing things like this, a pen and paper really helps me out keeping things clear about what happens.

    From my experience, one RPC to many is better than one to little.

    Good luck!
     
  4. unitygirl

    unitygirl

    Joined:
    May 25, 2010
    Posts:
    38
    thanks! I did manage to kinda solve the problem using RPC calls. Although I don't quite understand full of it.

    Thanks once again for replying <3