Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Sync Animation In Multiplayer

Discussion in 'Animation' started by thedragonyt_unity, Aug 11, 2018.

  1. thedragonyt_unity

    thedragonyt_unity

    Joined:
    Apr 27, 2018
    Posts:
    1
    Guys, help, I have a script to control the character, but in multiplayer, only the position synchronized, how to transfer animation in multiplayer?

    PlayerController script

    Code (CSharp):
    1. public class PlayerController : MonoBehaviour {
    2.  
    3.     public float moveSpeed;
    4.     //public Rigidbody theRB;
    5.     public float jumpForce;
    6.     public CharacterController controller;
    7.  
    8.     private Vector3 moveDirection;
    9.     public float gravityScale;
    10.  
    11.     public Animator anim;
    12.     public Transform pivot;
    13.     public float rotateSpeed;
    14.  
    15.     public GameObject playerModel;
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.         //theRB = GetComponent<Rigidbody>();
    20.         controller = GetComponent<CharacterController>();
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void LateUpdate () {
    25.         // theRB.velocity = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, theRB.velocity.y, Input.GetAxis("Vertical") * moveSpeed);
    26.  
    27.  
    28.  
    29.         /*if (Input.GetButtonDown("Jump"))
    30.         {
    31.             // theRB.velocity = new Vector3(theRB.velocity.x, junpForce, theRB.velocity.z);
    32.          
    33.         }*/
    34.  
    35.  
    36.         //moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, moveDirection.y, Input.GetAxis("Vertical") * moveSpeed);
    37.         float yStore = moveDirection.y;
    38.         moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
    39.         moveDirection = moveDirection.normalized * moveSpeed;
    40.         moveDirection.y = yStore;
    41.  
    42.         if (controller.isGrounded )
    43.         {
    44.  
    45.             moveDirection.y = 0f;
    46.  
    47.             if ((Input.GetButtonDown("Jump")))
    48.             {
    49.                 moveDirection.y = jumpForce;
    50.             }
    51.         }
    52.        
    53.  
    54.         moveDirection.y = moveDirection.y + (Physics.gravity.y * Time.deltaTime * gravityScale);
    55.  
    56.         controller.Move(moveDirection * Time.deltaTime);
    57.         //move player based on cam look dir
    58.         if (Input.GetAxis("Horizontal") !=0 || Input.GetAxis("Vertical") != 0)
    59.         {
    60.             transform.rotation = Quaternion.Euler(0f, pivot.rotation.eulerAngles.y, 0f);
    61.             Quaternion newRotation = Quaternion.LookRotation(new Vector3(moveDirection.x, 0f, moveDirection.z));
    62.             playerModel.transform.rotation = Quaternion.Slerp(playerModel.transform.rotation, newRotation, rotateSpeed * Time.deltaTime);
    63.         }
    64.  
    65.  
    66.         anim.SetBool("Grounded", controller.isGrounded);
    67.         anim.SetFloat("Speed", (Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal"))));
    68.     }
    69. }