PUN 2 door Animator problem
I'm making a game with PUN2 and and most everything is working but I have a automatic door that is causing me some problems. The Animator on the door is working player side for all characters, but it only plays across the server when whoever is hosting the walks to the door: But if the player who is not hosting walks into the door it will play for her but not across the server to all players:
I've done everything I can think of and am out of ideas. here is the script so you can see what's going on:
using UnityEngine;
using System.Collections;
public class Animation_door : MonoBehaviour {
Animator anim;
public AudioClip doorOpenClip;
public AudioClip doorCloseClip;
void Start()
{
anim = GetComponent<Animator>();
}
void OnTriggerEnter(Collider other) {
if(other.GetComponent<Collider>().tag=="Player")
{
anim.SetBool("Open",true);
GetComponent<AudioSource>().clip = doorOpenClip;
GetComponent<AudioSource>().Play();
}
}
void OnTriggerExit(Collider other) {
if(other.GetComponent<Collider>().tag=="Player")
{
anim.SetBool("Open",false);
GetComponent<AudioSource>().clip = doorCloseClip;
GetComponent<AudioSource>().Play();
}
}
}
Where is the netcode?
I guess the door only allow authority for server (server own the door and not allow client change animation state) and not client => client cannot call door open on everyone.
Answer by midgear · Oct 26, 2018 at 09:45 PM
Okay I got it working here is the code:
using UnityEngine;
using System.Collections;
using Photon.Pun;
[RequireComponent(typeof(PhotonView))]
[RequireComponent(typeof(PhotonAnimatorView))]
public class Animation_door : MonoBehaviourPun
{
//PhotonView photonView = PhotonView.Get(this);
public bool enter = false;
Animator anim;
public AudioClip doorOpenClip;
public AudioClip doorCloseClip;
void Start()
{
anim = GetComponent<Animator>();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
photonView.RPC("openDoor", RpcTarget.All, null);
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
photonView.RPC("closeDoor", RpcTarget.All, null);
}
}
[PunRPC]
void closeDoor()
{
//Debug.Log("Trigger Exit");
(enter) = false;
anim.SetBool("Open",false);
GetComponent<AudioSource>().clip = doorCloseClip;
GetComponent<AudioSource>().Play();
}
[PunRPC]
void openDoor()
{
//Debug.Log("Trigger Enter");
(enter) = true;
anim.SetBool("Open",true);
GetComponent<AudioSource>().clip = doorOpenClip;
GetComponent<AudioSource>().Play();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612180503im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
What's wrong with my script ? 0 Answers
Unity Photon Player Instantiation 1 Answer
Photon RPC Parameter NullReferenceException 1 Answer
I get an error message every time i run this script? Any ideas? 0 Answers
How do I sync health meters via Photon? 0 Answers