0

I want to animation of a static object which is in environment of unity multiplayer, are need to happen when player click on it. and also the animation would show every player who are connected to the server

public class AnimationCtr : NetworkBehaviour 
{

    Animator anim;
    bool canSpawn;

    int count;
    [SyncVar]
    bool flag;

    // Use this for initialization
    void Start () 
    {
        anim = GetComponent<Animator>();
        flag = false;
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, 100.0f))
            {
                if (hit.transform.gameObject.name.Equals("door") && (!flag))
                {
                    // anim.SetInteger("btnCount", 2);
                    Debug.Log(hit.transform.gameObject.name);
                    anim.SetInteger("btnCount", 2);
                    flag = true; 
                }
                else if (hit.transform.gameObject.name.Equals("door")&& (flag))
                {
                    anim.SetInteger("btnCount", 3);
                    flag = false;
                }
            }
        }
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
Jefhunt
  • 163
  • 1
  • 1
  • 9
  • Basically I want to door open close animation using unity multiplayer. Where if first player open door then second one can close that open door. – Jefhunt Sep 11 '18 at 09:20

1 Answers1

1

One problem is that you cannot set a [Synvar] on the client side.

From the [SyncVar] docu:

These variables will have their values sychronized from the server to clients

I would also only let the server decide whether the current value of flag is true or false.

public class AnimationCtr : NetworkBehaviour 
{

    Animator anim;
    bool canSpawn;

    int count;
    // no need for sync ar since flag is only needed on the server
    bool flag;

    // Use this for initialization
    void Start () 
    {
        anim = GetComponent<Animator>();
        flag = false;
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (!Physics.Raycast(ray, out hit, 100.0f)) return

            if (!hit.transform.gameObject.name.Equals("door")) return;

            // only check if it is a door
            // Let the server handel the rest
            CmdTriggerDoor();
        }
    }

    // Command is called by clients but only executed on the server
    [Command]
    private void CmdTriggerDoor()
    {
        // this will only happen on the Server

        // set the values on the server itself

        // get countValue depending on flag on the server
        var countValue = flag? 3 : 2;
        // set animation on the server
        anim.SetInteger("btnCount", countValue);
        // invert the flag on the server
        flag = !flag;

        //Now send the value to all clients
        // no need to sync the flag as the decicion is made only by server
        RpcSetBtnCount(countValue);
    }

    // ClientRpc is called by the server but executed on all clients
    [ClientRpc]
    private void RpcSetBtnCount(int countValue)
    {
        // This will happen on ALL clients

        // Skip the server since we already did it for him it the Command
        // For the special case if you are host -> server + client at the same time
        if (isServer) return;

        // set the value on the client
        anim.SetInteger("btnCount", countValue);
    }
}

Though, if you only want to animate if the door is opened or closed, I'ld rather use a bool parameter e.g. isOpen in the animator and simply change that instead (animator.SetBool()). And have two transitions between the two states:

Default -> State_Closed   -- if "isOpen"==true  --> State_Open
                          <-- if "isOpen"==false --

In your anmations you just save 1 single keyframe with the target position. Than on the transition you can set the transition duration -> how long it takes the door to open/close (they could even be different).

derHugo
  • 83,094
  • 9
  • 75
  • 115