Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. 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 at 5:02 AM.

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