Author Topic: [Solved] Animation RFC  (Read 4188 times)

ckrin

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
[Solved] Animation RFC
« on: October 15, 2013, 09:43:43 AM »
hey,

little problem here. i try to sync position and rotation and animation over network. so each player see's the others move correctly.
my problem is: all actions that other players do, trigger the animations on the player that first joined while position and rotation are syncd correctly.

this is the lower end of my charactercontroller script:
  1. ....
  2. ....
  3. ....
  4.  
  5.  
  6. switch(moveStatus)
  7.     {
  8.         case "idle":
  9.                                         if(!animCon.animation.IsPlaying("Attack02"))
  10.                                         animCon.animation.CrossFade("Idle")
  11.                                         ;break;
  12.         case "jump":
  13.                                 //      if(!animCon.animation.IsPlaying("Attack02"))
  14.                                         animCon.animation.CrossFade("Idle");
  15.                                         break;
  16.         case "walking":
  17.                                         //if(!animCon.animation.IsPlaying("Attack02"))
  18.                                         animCon.animation.CrossFade("Run");
  19.                                         break;
  20.         case "running":
  21.                                         //if(!animCon.animation.IsPlaying("Attack02"))
  22.                                         animCon.animation.CrossFade("Run");
  23.                                         break;
  24.         case "sidewalking_r":
  25.                                         //if(!animCon.animation.IsPlaying("Attack02"))
  26.                                         animCon.animation.CrossFade("Run");
  27.                                         break;
  28.         case "siderunning_r":
  29.                                         //if(!animCon.animation.IsPlaying("Attack02"))
  30.                                         animCon.animation.CrossFade("Run");
  31.                                         break;
  32.         case "sidewalking_l":
  33.                                         //if(!animCon.animation.IsPlaying("Attack02"))
  34.                                         animCon.animation.CrossFade("Run");
  35.                                         break;
  36.         case "siderunning_l":
  37.                                         //if(!animCon.animation.IsPlaying("Attack02"))
  38.                                         animCon.animation.CrossFade("Run");
  39.                                         break;
  40.         case "backwalking":
  41.                                         //if(!animCon.animation.IsPlaying("Attack02"))
  42.                                         animCon.animation.CrossFade("Run");
  43.                                         break;
  44.                                        
  45.         case "backrunning":
  46.                                         //if(!animCon.animation.IsPlaying("Attack02"))
  47.                                          animCon.animation.CrossFade("Run");
  48.                                          break;
  49.         case "melee_attack":
  50.                                         animCon.animation.Play("Attack02");
  51.                                         break;
  52.         case "dead":    
  53.                                         if(deadRepAnim)
  54.                                         return;
  55.                                         Debug.Log("Dead anim");
  56.                                         animCon.animation.Play("Dead");
  57.                                         deadRepAnim=true;
  58.                                         break;  
  59.     }
  60.    
  61.     //Send animations
  62.     tno.Send("PlayAnimation", Target.All,transform.position, transform.rotation, moveStatus);
  63. }//tno.isMine  
  64. }//Update
  65.  
  66. @RFC
  67. function PlayAnimation(pos:Vector3, rot:Quaternion,animation:String){
  68.    
  69.    transform.position=pos;
  70.    transform.rotation=rot;
  71.    moveStatus=animation;
  72. }

« Last Edit: October 20, 2013, 02:58:59 PM by ckrin »

addebooi

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: Animation RFC
« Reply #1 on: October 15, 2013, 10:45:43 AM »
Where does it say that the other players play the animation sent to them? You should write the "switch(moveStatus)" outside the tno.IsMine

ckrin

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: Animation RFC
« Reply #2 on: October 15, 2013, 11:04:24 AM »
thanks for the fast answer.

okay, did that, but doesnt help.

maybe you see my mistake in full script:

  1. import TNet;
  2.  
  3.  
  4. //Var definition
  5. public var swimming:boolean = false;                //Can be triggert to slow down the movements (like when u swim)
  6. public var moveStatus:String = "idle";              //movestatus for animations
  7.  
  8. //Movement speeds
  9.  var jumpSpeed:float = 8.0;                  //Jumpspeed / Jumpheight
  10.  var gravity:float = 20.0;                   //Gravity for jump
  11.  var runSpeed:float = 10.0;                  //Speed when the Character is running
  12.  var walkSpeed:float = 4.0;                  //Speed when the Character is walking (normal movement)
  13.  var rotateSpeed:float = 250.0;              //Rotationspeed of the Character
  14.  var walkBackMod:float = 0.75;               //Speed in Percent for walk backwards and sidewalk
  15.  
  16. //Internal vars to work with
  17. private var speedMod:float = 0.0;                   //temp Var for Speedcalculation
  18. private var grounded:boolean = false;               //temp var if the character is grounded
  19. private var moveDirection:Vector3 = Vector3.zero;   //move direction of the Character
  20. private var isWalking:boolean = false;              //toggle var between move and run
  21. private var jumping:boolean = false;                //temp var for jumping
  22. private var mouseSideButton:boolean = false;        //temp var for mouse side buttons
  23. private var pbuffer:float = 0.0;                    //Cooldownpuffer for SideButtons
  24. private var coolDown:float = 0.5;                   //Cooldowntime for SideButtons
  25.  
  26.  
  27. //ref
  28. var refToPlayerStatus:Player_Status;
  29. refToPlayerStatus=GetComponent(Player_Status);
  30.  
  31. //anims
  32. var animClass:String;
  33. var animCon:GameObject;
  34. var deadRepAnim:boolean=false;
  35.  
  36. //Network
  37. private var tno: TNObject;
  38. function setAnimClass(){
  39.  
  40.         switch(animClass)
  41.         {
  42.         case "goblin": animCon=GameObject.Find("mon_goblinWizard_");break;
  43.         case "orc": animCon=GameObject.Find("mon_orcWarrior_");break;
  44.         case "orge": animCon=GameObject.Find("mon_orgeHitter_");break;
  45.         case "trol": animCon=GameObject.Find("mon_trolCurer_");break;
  46.         default:animCon=GameObject.Find("mon_goblinWizard_");break;
  47.         }
  48. }
  49.  
  50. function Start(){
  51. setAnimClass();
  52.  
  53. tno=GetComponent(TNObject);
  54.  
  55.  
  56. }
  57.  
  58. //Every Frame
  59. function Update ()
  60. {
  61. if(tno.isMine){
  62.  
  63.  
  64.  
  65.  
  66.  
  67.     //Set idel animation
  68.     moveStatus = "idle";
  69.          
  70.     // Hold "Run" to run
  71.     isWalking = Input.GetAxis("Run") ? false : true;
  72.      
  73.     // Only allow movement and jumps while grounded
  74.     if(grounded) {
  75.          
  76.         //movedirection
  77.         moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
  78.          
  79.         //pushbuffer to avoid on/off flipping
  80.         if(pbuffer>0)
  81.             pbuffer -=Time.deltaTime;
  82.         if(pbuffer<0)pbuffer=0;
  83.                        
  84.         //Automove Sidebuttonmovement
  85. /*        if(Input.GetAxis("Toggle Move") && pbuffer == 0){
  86.             pbuffer=coolDown;
  87.             mouseSideButton = !mouseSideButton;
  88.         }
  89.         if(mouseSideButton && ((Input.GetAxis("Vertical") != 0) || Input.GetButton("Jump") || (Input.GetMouseButton(0) && Input.GetMouseButton(1)) ))
  90.             mouseSideButton = false;        
  91.     */    
  92.         //L+R MouseButton Movement
  93.         if (Input.GetMouseButton(0) && Input.GetMouseButton(1) || mouseSideButton)
  94.             moveDirection.z += 1;
  95.         if (moveDirection.z > 1)
  96.             moveDirection.z = 1;
  97.                  
  98.          
  99.         //Strafing move (like Q/E movement    
  100.     //    moveDirection.x -= Input.GetAxis("Strafing");
  101.              
  102.           // if moving forward and to the side at the same time, compensate for distance
  103.           if(Input.GetMouseButton(1) && Input.GetAxis("Horizontal") && Input.GetAxis("Vertical")) {
  104.             moveDirection *= .7;
  105.           }
  106.                            
  107.         //Speedmodification / is moving forward or side/backward
  108.         speedMod = ((Input.GetAxis("Vertical") < 0) || (Input.GetMouseButton(1) && Input.GetAxis("Horizontal"))) ? walkBackMod : 1.0;
  109.          
  110.         //Use run or walkspeed
  111.         moveDirection *= isWalking ? walkSpeed * speedMod : runSpeed * speedMod;
  112.          
  113.         //reduce movement by 70% when swimming is toggled    
  114.         moveDirection*= swimming ? 0.0 : 1;
  115.                    
  116.         // Jump!
  117.         if(Input.GetButton("Jump")){
  118.             jumping = true;
  119.             moveDirection.y = jumpSpeed;
  120.         }
  121.          
  122.         //movestatus normal movement (for animations)              
  123.         if((moveDirection.x == 0 ) && (moveDirection.z == 0))
  124.             moveStatus = "idle";            
  125.         if(moveDirection.z > 0)
  126.             moveStatus = isWalking ? "walking" : "running";
  127.         if(moveDirection.z < 0)
  128.             moveStatus = isWalking ? "backwalking" : "backrunning";        
  129.         if(moveDirection.x > 0)
  130.             moveStatus = isWalking ? "sidewalking_r" : "siderunning_r";
  131.         if(moveDirection.x < 0)
  132.             moveStatus = isWalking ? "sidewalking_l" : "siderunning_l";    
  133.          
  134.         //movestatus swim movement (for animations)              
  135.         if(swimming){
  136.             if((moveDirection.x == 0 ) && (moveDirection.z == 0))
  137.                 moveStatus = "swimidle";            
  138.             if(moveDirection.z > 0)
  139.                 moveStatus = isWalking ? "swim" : "swimfast";
  140.             if(moveDirection.z < 0)
  141.                 moveStatus = isWalking ? "backswim" : "backswimfast";        
  142.             if(moveDirection.x > 0)
  143.                 moveStatus = isWalking ? "sideswim_r" : "sideswimfast_r";
  144.             if(moveDirection.x < 0)
  145.                 moveStatus = isWalking ? "sidewswim_l" : "sideswimfast_l";    
  146.             if(jumping)
  147.                 moveStatus = "swimup";                    
  148.         }  
  149.          
  150.         //transform direction
  151.         moveDirection = transform.TransformDirection(moveDirection);        
  152.  
  153.     }
  154.     // Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
  155.     if(Input.GetMouseButton(1)) {
  156.         transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
  157.     } else {
  158.         transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
  159.     }
  160.  
  161.     //Apply gravity
  162.     moveDirection.y -= gravity * Time.deltaTime;
  163.      
  164.     //Move controller
  165.     var controller:CharacterController = GetComponent(CharacterController);
  166.     var flags = controller.Move(moveDirection * Time.deltaTime);
  167.     grounded = (flags & CollisionFlags.Below) != 0;
  168.      
  169.     //Reset jumping after landing
  170.     jumping = grounded ? false : jumping;
  171.  
  172.     //movestatus jump/swimup (for animations)      
  173.     if(jumping)
  174.         moveStatus = "jump";
  175.     if(jumping && swimming)
  176.         moveStatus = "swimup";
  177.        
  178.     //health (for anyimations)
  179.     if(refToPlayerStatus.health<=0)
  180.     moveStatus = "dead";
  181.     else
  182.     deadRepAnim=false;
  183.     //playerActions    
  184.    
  185.     if(Input.GetButton("MeleeAttack")&& moveStatus!="melee_attack" && !animCon.animation.IsPlaying("Attack02"))
  186.     {
  187.         moveStatus="melee_attack";
  188.         meleeAttack();
  189.     }
  190.    
  191.     //Send animations
  192.     tno.Send("PlayAnimation", Target.All,transform.position, transform.rotation, moveStatus);
  193.         }//tno.isMine  
  194.         switch(moveStatus)
  195.     {
  196.         case "idle":
  197.                                         if(!animCon.animation.IsPlaying("Attack02"))
  198.                                         animCon.animation.CrossFade("Idle")
  199.                                         ;break;
  200.         case "jump":
  201.                                 //      if(!animCon.animation.IsPlaying("Attack02"))
  202.                                         animCon.animation.CrossFade("Idle");
  203.                                         break;
  204.         case "walking":
  205.                                         //if(!animCon.animation.IsPlaying("Attack02"))
  206.                                         animCon.animation.CrossFade("Run");
  207.                                         break;
  208.         case "running":
  209.                                         //if(!animCon.animation.IsPlaying("Attack02"))
  210.                                         animCon.animation.CrossFade("Run");
  211.                                         break;
  212.         case "sidewalking_r":
  213.                                         //if(!animCon.animation.IsPlaying("Attack02"))
  214.                                         animCon.animation.CrossFade("Run");
  215.                                         break;
  216.         case "siderunning_r":
  217.                                         //if(!animCon.animation.IsPlaying("Attack02"))
  218.                                         animCon.animation.CrossFade("Run");
  219.                                         break;
  220.         case "sidewalking_l":
  221.                                         //if(!animCon.animation.IsPlaying("Attack02"))
  222.                                         animCon.animation.CrossFade("Run");
  223.                                         break;
  224.         case "siderunning_l":
  225.                                         //if(!animCon.animation.IsPlaying("Attack02"))
  226.                                         animCon.animation.CrossFade("Run");
  227.                                         break;
  228.         case "backwalking":
  229.                                         //if(!animCon.animation.IsPlaying("Attack02"))
  230.                                         animCon.animation.CrossFade("Run");
  231.                                         break;
  232.                                        
  233.         case "backrunning":
  234.                                         //if(!animCon.animation.IsPlaying("Attack02"))
  235.                                          animCon.animation.CrossFade("Run");
  236.                                          break;
  237.         case "melee_attack":
  238.                                         animCon.animation.Play("Attack02");
  239.                                         break;
  240.         case "dead":    
  241.                                         if(deadRepAnim)
  242.                                         return;
  243.                                         Debug.Log("Dead anim");
  244.                                         animCon.animation.Play("Dead");
  245.                                         deadRepAnim=true;
  246.                                         break;  
  247.     }
  248.    
  249.  
  250.  
  251. }
  252.  
  253. @RFC
  254. function PlayAnimation(pos:Vector3, rot:Quaternion,animation:String){
  255.    
  256.    transform.position=pos;
  257.    transform.rotation=rot;
  258.    moveStatus=animation;
  259. }
  260.  
  261.  


addebooi

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: Animation RFC
« Reply #3 on: October 15, 2013, 12:21:49 PM »
1. You shouldent send the position of the player every frame, you should be sending the positioning 4-8 times every seconds and then send the vector the player is moving at abit more often, let's say 20-25 times. There on you should let the client who recieved the vectores for the movement making the calculation how the player should move(less data send). And then the client checks what kind of thing the other players are doing according to their movements, and if you want them to die or hit or something similiar so should you send that in a diffrent pakage(my opinion).

2. should you really set the moveStatus to idle everytime it runs through the script?
Otherwise so couldn't I see any problems with your script.

ckrin

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: Animation RFC
« Reply #4 on: October 15, 2013, 04:05:58 PM »
thanks for the reply. your right, moveStatus=idle makes no sens in the updatefunction.
your answer causes more questions.

1. in my case i need to sync moveDirection right? Means i send RFC when some user Input happens, right? How does this work since moveDirection is inside of tno.isMine, wouldnt incoming data be ignored? just had a test with tnserver.exe via public adress and the players & camera's  most of the time twiched hard. calculating moveDirection client side will fix this right? Even my move speed is reduced alot :s


2. animations are still only played on character that joins first :/


addebooi

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 26
    • View Profile
Re: Animation RFC
« Reply #5 on: October 15, 2013, 04:56:19 PM »
1. You could as well write something similar outside the tno.IsMine "controller.Move(moveDirection * Time.deltaTime)", Then just send the RFC and sets the moveDirection on the object as wanted and let the other clients to calculate where to move. And because of the "delay" so will you all so have to set the position/rotation like 4 times a second as i mentioned earlier. Ohh yeah. Why your own character twists can be because you are sending the RFC to all. You shouldn't do that, Use the Target.Others for transmitting your position and your moveDirection. And if you want your characters movespeed do be diffrent sometimes so should you calculate the moveDirections * speed or whatever before you transmite it over the network.

2. As understand: When other players "move" the move animations only plays for the first person who joined the channel?

ckrin

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: Animation RFC
« Reply #6 on: October 15, 2013, 05:16:45 PM »
right.
Quote
if player A joins, he can move with animations.
if player B joins and moves he plays animations on player A's avatar . Player A movement doesnt trigger animations for himself from now on :S

SOLVED! when instantiating my animation controller seeked for the ameObjects that contains the different races and animations (4 child objects). this causes that second and third player always found the child objects of the first player... i solved it by seeking with
  1. transform.Find("goblin").gameObject
wich only can access child objects.

----------
Quote
how can i set my send rate per seconds without the autosync? my syncPlayerPosRot() RFC is still in update function, sending a huge amount of packets  :-\

SOLVED! got it working with Time.deltaTime;

« Last Edit: October 16, 2013, 11:15:23 AM by ckrin »