나는 모든 선수는이 스크립트와 함께 현장 에서 게임 오브젝트를 이동하고 던질 수 있도록하려면 :모든 플레이어의 스크립트 작동 방법 Unity Photon?
[RequireComponent(typeof(PhotonView))]
public class PickupObjectNetwork : Photon.MonoBehaviour {
public GameObject mainCamera;
public bool carrying;
GameObject carriedObject;
public float distance;
public float smooth;
public float throwPower = 10f;
private float chargeTime = 0f;
// Use this for initialization
void Start() {
mainCamera = GameObject.FindWithTag("MainCamera");
}
// Update is called once per frame
void Update() {
if(photonView.isMine)
{
if(carriedObject == null)
{
carrying = false;
}
if(gameObject == null)
return;
if(carrying) {
photonView.RPC("carry", PhotonTargets.AllBuffered);
photonView.RPC("checkDrop", PhotonTargets.AllBuffered);
if (Input.GetMouseButtonUp (0)) {
float pushForce = chargeTime * 10;
pushForce = Mathf.Clamp (pushForce, 1, 20);
photonView.RPC("throwObject", PhotonTargets.AllBuffered, pushForce);
chargeTime = 0;
}
} else {
if(Input.GetKeyDown (KeyCode.E)) {
photonView.RPC("pickup", PhotonTargets.AllBuffered);
}
}
if(Input.GetMouseButton(0)){
chargeTime += Time.deltaTime;
}
}
}
[PunRPC]
void carry() {
if (carriedObject == null)
{
return;
}
Debug.Log("picked up object with id: " + carriedObject.GetComponent<PhotonView>().viewID);
carriedObject.transform.position = Vector3.Lerp (carriedObject.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
carriedObject.transform.rotation = Quaternion.identity;
}
[PunRPC]
void pickup() {
int x = Screen.width/2;
int y = Screen.height/2;
Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x,y));
RaycastHit hit;
if(Physics.Raycast(ray, out hit)) {
Pickupable p = hit.collider.GetComponent<Pickupable>();
if(p != null && hit.distance < 5) {
carrying = true;
carriedObject = p.gameObject;
p.gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
p.gameObject.GetComponent<Rigidbody>().useGravity = false;
}
}
}
[PunRPC]
void checkDrop() {
if(Input.GetKeyDown (KeyCode.E)) {
dropObject();
}
}
[PunRPC]
void dropObject() {
carrying = false;
carriedObject.gameObject.GetComponent<Rigidbody>().useGravity = true;
carriedObject = null;
}
[PunRPC]
void throwObject(float pushForce){
carrying = false;
if(carriedObject.tag != "BoxMultiplayer")
carriedObject.tag = "Projectile";
carriedObject.GetComponent<Rigidbody>().AddForce(Camera.main.transform.forward * pushForce * 200);
carriedObject.gameObject.GetComponent<Rigidbody>().useGravity = true;
carriedObject = null;
}
}
내가 ( PhotonNetwork.InstantiateSceneObject를 사용하는 장면 객체를 생성); 이 스크립트는 마스터 클라이언트에서만 작동하며 비 마스터 클라이언트는 개체를 이동할 수 없습니다.
또한이 스크립트를 무작위로 사용해 보았습니다. http://pastebin.com/7DgjZ9U7 AllocateViewId()를 사용하는 방법을 알지 못합니다. 이 스크립트를 사용하면 두 플레이어가 모두 씬 개체를 이동할 수 있지만 개체 이동은 이 아닙니다 (다른 플레이어의 개체 위치가 업데이트되지 않음). 또한 플레이어 중 하나가 gameObject를 벽에 던져 PhotonNetwork.Destroy()로 새로운 충돌을 없애면 이 플레이어는 다른 플레이어와 고정됩니다. 내 질문이 지저분하지 않기를 바랍니다. 미리 감사드립니다!
답변 주셔서 감사합니다. 이전에 photonView.RequestOwnership()을 시도했지만 성공하지 못했습니다. 또한 DemoChangeOwner-Scene을 확인하고 pickup() 함수에 photonView.RequestOwnership()을 넣었습니다.하지만 didn' 일하지 마라. 제발 말해 줄 수 있어요, 어떤 기능을 써야합니까? –
플레이어가 소유자가 아닌 객체의 변형을 변경해야 할 때마다 호출해야합니다. 개체 소유자를 변경하려면 OwnershipOption을 요청 또는 인계로 설정해야합니다. – Jansen
내 gameObject의 photonView가 요청하도록 설정되어 있습니다. 또한 photonView.RequestOwnership()을 carry() function => Update() 함수에 넣었지만 나쁘지는 않지만 테스트하고 싶습니다. 그리고 여전히 작동하지 않습니다. 두 플레이어는 대상을 잡으려고 시도한 후에 서로를 동결합니다. ................ Debug.Log : Ev OwnershipRequest : {245 = System.Int32 [], 254 = 1} ViewID : 1001 보낸 사람 : 1 시간 : 709 ..... ............. Ev OwnershipRequest PhotonView.ownershipTransfer : 인수 .ownerId : 1 isOwnerActive : True. 이 고객의 선수 : # 01 ''{} –