Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

problem with sliding/scooting character

Discussion in 'Multiplayer' started by rmbunity, Feb 2, 2008.

  1. rmbunity

    rmbunity

    Joined:
    Dec 1, 2007
    Posts:
    32
    Hi:

    I am using the Goober character from Unity's CharacterAnimation
    demo in a test of my own. I do a Network.Instantiate of the character
    and have it show up in two different windows. And when I press the
    arrow keys in the window that "owns" the character, both copies
    move around the scene. In the window that owns the character, it
    is fully animated as desired. However, in the other window, the
    character merely scoots across the terrain rather than "walking",
    i.e. rather than being animated. I have looked at the CarRacing
    demo in the Networking sample project because I thought it might
    give me some hints on how to fix the problem. But so far I am
    missing something from all the demos I have examined. Helpful hints
    would be greatly appreciated.

    Thanks.
    --ralph
     
  2. larus

    larus

    Unity Technologies

    Joined:
    Oct 12, 2007
    Posts:
    277
    You probably need to specifically sync the animation of the character. In the ThirdPerson scene in the example project the animation state of the character is synched over the network when it changes. Check out the ThirdPersonSimpleAnimation.js script and NetworkSyncAnimation.cs.
     
  3. rmbunity

    rmbunity

    Joined:
    Dec 1, 2007
    Posts:
    32
    Thanks for the reply. This did help me solve the problem
    with that character. I now have a similar problem with
    another character that I had hoped I could solve in the
    same way. Ah well, perhaps I will post again if I learn enough
    to ask a reasonable question.
    Thanks again.
    --ralph
     
  4. bmeyer01

    bmeyer01

    Joined:
    Jul 20, 2007
    Posts:
    9
    You know, I am also having the same problem.

    The character on my project is "scooting" too. Specifically, it is looping the "idle" animation.

    I went through the Network sample, but for some reason that file performs the same way.

    When I build the Network sample the Lerps "walk" cycle is looping on the networked machine. It doesn't matter if I move the character or not. No "idle" or any of the other animations seem to be working with the sample.

    I wonder if I am doing something wrong.

    Basically I build the sample "binary standalone" and test it between a macbook pro and a powerbook g4.

    All controls, chat, etc work properly. But the animation does not sync on the 3rd person sample.

    Does the Network sample work correctly for anyone else?
     
  5. bmeyer01

    bmeyer01

    Joined:
    Jul 20, 2007
    Posts:
    9
    I forgot to mention that the TPS-Auth sample also has the same problem.

    The Lerpz "walk cycle" is looping. None of the other animations work on networked machines.

    It almost seems that this bit of code does not work at all.

    Code (csharp):
    1. if (stream.isWriting)
    2.         {
    3.             char ani = (char)currentAnimation;
    4.             stream.Serialize(ref ani);
    5.         }
    6.  
     
  6. AngryAnt

    AngryAnt

    Keyboard Operator Moderator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    What I do is serialize a state variable which I then use to decide which animation to play.
     
  7. bmeyer01

    bmeyer01

    Joined:
    Jul 20, 2007
    Posts:
    9
    That sounds like an interesting technique. . .

    I'm pretty slow when it comes to programming, could you please walk me through your process?
     
  8. AngryAnt

    AngryAnt

    Keyboard Operator Moderator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    I'm using an enum to specify the state. Not sure how enums are used in javascript - if that is the language you use, but here goes:

    We first define the enum which specifies the states the character can be in and set it to idle as default:

    Code (csharp):
    1. enum PlayerState{ Idle, Run, Aim };
    2. PlayerState state = PlayerState.Idle;
    Now when player input prompts the character to move, we change the player state accordingly:

    Code (csharp):
    1. state = PlayerState.Run;
    This is used in our update to set the animation:

    Code (csharp):
    1. void Update()
    2. {
    3.     Vector3 pos;
    4.  
    5.     switch( state )
    6.     {
    7.         case PlayerState.Idle:
    8.             animation.CrossFade( "idle" );
    9.         break;
    10.         case PlayerState.Run:
    11.             animation.CrossFade( "run" );
    12.         break;
    13.         case PlayerState.Aim:
    14.             animation.CrossFade( "aim" );
    15.         break;
    16.     }
    17. }
    Finally we make sure the state is synced. However enums do not serialize so new need to convert to int beforehand - like so:

    Code (csharp):
    1. int convertedState;
    2. if( bitStream.isWriting )
    3. {
    4.     convertedState = ( int )state;
    5.     bitStream.Serialize( ref convertedState );
    6. }
    7. else
    8. {
    9.     bitStream.Serialize( ref convertedState );
    10.     state = ( PlayerState )networkedState;
    11. }
     
  9. bmeyer01

    bmeyer01

    Joined:
    Jul 20, 2007
    Posts:
    9
    Wow thank you. That really helps me a little further. I sort of combined your code to the "NetworkSyncAnimation.cs" and "ThirdPersonSimpleAnimation.js" scripts on the Lerps network demo.

    I'm still getting sliding characters when I network a powerbook with a macbook.

    Clearly I am doing something wrong here. The network demo still has sliding characters on my machines.

    Would it be possible to provide me a little more guidance on this?

    Everything else seems to network perfectly. Animation is my only problem.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5.  
    6. public class NetworkSyncAnimation : MonoBehaviour {
    7.    
    8.     public enum PlayerState
    9.     {
    10.         idle = 0,
    11.         walk,
    12.         run,
    13.         jump
    14.     }
    15.    
    16.     public PlayerState state = PlayerState.idle;
    17.        
    18.     // Update is called once per frame
    19.     void Update () {
    20.         Vector3 pos;
    21.         switch( state )
    22.         {
    23.             case PlayerState.idle: animation.CrossFade( "idle" );
    24.             break;
    25.             case PlayerState.walk: animation.CrossFade( "walk" );
    26.             break;
    27.             case PlayerState.run: animation.CrossFade( "run" );
    28.             break;
    29.             case PlayerState.jump: animation.CrossFade( "jump" );
    30.             break;
    31.             }      
    32.         }
    33.                
    34.     // Network Stuff
    35.     void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
    36.         {
    37.             int convertedState;
    38.             if(stream.isWriting )
    39.             {
    40.                 convertedState = ( int )state;
    41.                 stream.Serialize( ref convertedState );
    42.                 }
    43.                 else
    44.                 {
    45.                 convertedState = ( int )0;
    46.                 stream.Serialize( ref convertedState );
    47.                 state = ( PlayerState )convertedState;
    48.  
    49.                 }
    50.         }
    51. }
    52.  
    Here's what's in the "ThirdPersonSimpleAnimation" script now

    Code (csharp):
    1.  
    2.         else if (currentSpeed > 0.1)
    3.     {
    4.         animation.CrossFade("walk");
    5.         animation.Blend("jumpland", 0);
    6.         SendMessage("state", "PlayerState.walk");
    7.     }
    8.  
    9.  
     
  10. bmeyer01

    bmeyer01

    Joined:
    Jul 20, 2007
    Posts:
    9
    Ok I am still stumped here.

    Again, the lerps character in the network demo seems to loop the walk cycle on all networked machines (powerbook and macbook pro).

    Did this network demo actually work for anyone else?

    Thanks in advance for any help or insight.
     
  11. AngryAnt

    AngryAnt

    Keyboard Operator Moderator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    The idea is that you completely control animation by setting the state and thus only change the animation in the update which responds to the current state. Therefore I do not get why you're modifying the animation in your "ThirdPersonSimpleAnimation" script - this seems to counter the idea. However the snippet isn't descriptive enough to let me conclude this.
     
  12. bmeyer01

    bmeyer01

    Joined:
    Jul 20, 2007
    Posts:
    9
    Sorry for the late reply, I'm falling behind on other projects...

    Your point does make sense to me. I guess what I am trying to do is make sense on why the the Lerps character in the Third Person Networking sample (from the resources section on the unity website) does not seem to play the "walk, run, jump, etc" animations properly on networked machines.

    I tested this on different machines and on different networks and I always seem to get the same result (looping walk cycle only, no other animations).

    Looking through the code in the sample, it seemed to me that the "ThirdPersonSimpleAnimation.js: contains this:

    Code (csharp):
    1. // Fade in run
    2.     if (currentSpeed > marioController.walkSpeed)
    3.     {
    4.         animation.CrossFade("run");
    5.         // We fade out jumpland quick otherwise we get sliding feet
    6.         animation.Blend("jumpland", 0);
    7.         SendMessage("SyncAnimation", "run");
    8.     }
    9.  
    My thought was that this:

    Code (csharp):
    1. SendMessage("SyncAnimation", "run");
    tells the "NetworkSyncAnimation.cs" script to change the animation state here:

    Code (csharp):
    1. public AniStates currentAnimation = AniStates.idle;
    2.     public AniStates lastAnimation = AniStates.idle;
    3.    
    4.     public void SyncAnimation(String animationValue)
    5.     {
    6.         currentAnimation = (AniStates)Enum.Parse(typeof(AniStates), animationValue);
    7.     }
    8.  
    So basically what I did was substitute the sample code with the code you posted (thank you for this by the way)

    So I guess my main question was:

    1) Does the network sample work correctly for anyone else?
    2) If so, what exactly are you doing to make it work properly?

    And my new question is:

    1)Clearly I am not using your code properly. Are you telling me that the "ThirdPersonSimpleAnimation.js" is actually not necessary to make the network animations work properly?


    Thank you again for your help on this.