photonView.RequestOwnership();它不起作用(unity3d)

时间:2019-02-14 18:54:30

标签: c# unity3d

我正在创建一个多人游戏,我想插入抓斗系统,我知道我必须放入photonView.RequestOwnership();。 但是我做到了,它不会给我错误,只有当我拿走对象时才让我不走,而是如果我不放photonView.RequestOwnership();它很棒,我该怎么解决?

在统一3d上使用光子网络

在photonview上,我已经戴上了“ TakeOver”

这是文件的代码

GrabObjects.cs photonView.RequestOwnership();我将其放在函数中,这样可以吗?你做得好吗?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

[RequireComponent(typeof (PhotonView))]
public class GrabObjects : Photon.MonoBehaviour
{


public float grabPower = 10.0f; 
public float throwPower = 25.0f;
public float RayDistance = 3.0f;
public LayerMask layerMask;
private bool grab, drop = false;

public Transform pos;
public float adjust;
Rigidbody obj;

void Update()
{ 

if (Input.GetKeyDown(KeyCode.Mouse1))
{
RaycastHit hit;
if(Physics.Raycast(transform.position, transform.forward, out hit, RayDistance, layerMask.value))
{
Pickable pickable = hit.collider.GetComponent();
if(pickable)
{
/// I put it here 
photonView.RequestOwnership();
grab = true;
obj = hit.rigidbody;
obj.isKinematic = true;
obj.GetComponent().enabled = false;
obj.transform.parent = pos;
} 
}
}   

if (Input.GetKeyUp(KeyCode.Mouse1))
{ 
if(grab)
{
StartCoroutine(PrepareToDrop(3f));
}   
}

if (Input.GetKeyDown(KeyCode.Mouse0))
{ 
if(grab)
{
StartCoroutine(PrepareToDrop(throwPower));
}   
}

if(grab)
{   
obj.transform.position = Vector3.Lerp(obj.transform.position, pos.position + (pos.transform.forward * adjust) - (pos.transform.up * 0.4f), Time.deltaTime * grabPower); 
obj.transform.rotation = Quaternion.Lerp(obj.transform.rotation, pos.rotation, Time.deltaTime * 5f);
}
} 

IEnumerator PrepareToDrop(float power)
{
RaycastHit hits;
while(!drop)
{
if(Physics.Raycast(transform.position, transform.forward, out hits, 1.5f, layerMask.value)) 
drop = false;
else 
drop = true;
yield return null;
}

obj.transform.parent = null;
obj.isKinematic = false;
obj.GetComponent().enabled = true;
obj.velocity = transform.forward * power;
drop = grab = false;
}
}

这里是另一个,这是空的,放置在对象参考点中,以使脚本理解要获取该对象

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof (PhotonView))]
public class Pickable : Photon.MonoBehaviour {

}

0 个答案:

没有答案