C# 如何使脚本适用于所有玩家?
我希望所有玩家都能在场景中移动和投掷游戏对象 使用此脚本:C# 如何使脚本适用于所有玩家?,c#,unity3d,unity5,photon,C#,Unity3d,Unity5,Photon,我希望所有玩家都能在场景中移动和投掷游戏对象 使用此脚本: [RequireComponent(typeof(PhotonView))] 公共类PickupObjectNetwork:Photon.MonoBehavior{ 公共游戏机; 公共厕所; 游戏对象承载对象; 公众浮标距离; 公共交通畅通; 公共浮子通过功率=10f; 私人浮动充电时间=0f; //用于初始化 无效开始(){ mainCamera=GameObject.FindWithTag(“mainCamera”); } //每帧
[RequireComponent(typeof(PhotonView))]
公共类PickupObjectNetwork:Photon.MonoBehavior{
公共游戏机;
公共厕所;
游戏对象承载对象;
公众浮标距离;
公共交通畅通;
公共浮子通过功率=10f;
私人浮动充电时间=0f;
//用于初始化
无效开始(){
mainCamera=GameObject.FindWithTag(“mainCamera”);
}
//每帧调用一次更新
无效更新(){
if(photonView.isMine)
{
if(carriedObject==null)
{
携带=假;
}
如果(游戏对象==null)
返回;
如果(携带){
RPC(“携带”,PhotonTargets.AllBuffered);
RPC(“checkDrop”,PhotonTargets.AllBuffered);
if(Input.GetMouseButtonUp(0)){
浮动推力=充电时间*10;
推力=主夹具(推力,1,20);
RPC(“throwObject”,PhotonTargets.AllBuffered,pushForce);
充电时间=0;
}
}否则{
if(Input.GetKeyDown(KeyCode.E)){
RPC(“拾取”,PhotonTargets.AllBuffered);
}
}
if(输入。GetMouseButton(0)){
chargeTime+=Time.deltaTime;
}
}
}
[扑克牌]
无效进位(){
if(carriedObject==null)
{
返回;
}
Log(“拾取id为“+carriedObject.GetComponent().viewID”的对象);
carriedObject.transform.position=Vector3.Lerp(carriedObject.transform.position,Main Camera.transform.position+Main Camera.transform.forward*距离、时间、增量*平滑);
carriedObject.transform.rotation=四元数.identity;
}
[扑克牌]
无效拾取(){
int x=屏幕宽度/2;
int y=屏幕高度/2;
Ray-Ray=mainCamera.GetComponent().ScreenPointToRay(新矢量3(x,y));
雷卡斯特击中;
如果(物理.光线投射(光线,出击)){
Pickupable p=hit.collider.GetComponent();
如果(p!=null&&hit.distance<5){
携带=正确;
carriedObject=p.gameObject;
p、 gameObject.GetComponent().velocity=Vector3.0;
p、 gameObject.GetComponent().useGravity=false;
}
}
}
[扑克牌]
void checkDrop(){
if(Input.GetKeyDown(KeyCode.E)){
dropObject();
}
}
[扑克牌]
void dropObject(){
携带=假;
carriedObject.gameObject.GetComponent().useGravity=true;
carriedObject=null;
}
[扑克牌]
void throwObject(浮动推力){
携带=假;
如果(carriedObject.tag!=“BoxMultiplayer”)
carriedObject.tag=“射弹”;
carriedObject.GetComponent().AddForce(Camera.main.transform.forward*pushForce*200);
carriedObject.gameObject.GetComponent().useGravity=true;
carriedObject=null;
}
}
我使用
PhotonNetwork.instantialeSceNeObject();此脚本仅适用于
主客户端,非主客户端不能移动对象
我还尝试了这个脚本(随机):
我真的不知道如何使用AllocateView()。当我用这个的时候
在脚本中,两个玩家都可以移动场景对象,但对象移动不是
已同步(其他玩家不更新对象位置)。
另外,当一名玩家将游戏对象扔进
墙,用于破坏与PhotonNetwork的新碰撞。Destroy(),
此玩家为其他玩家冻结。
希望我的问题不是超级混乱。提前谢谢
如果没有对象权限的玩家处理该对象,更改将不会同步到其他玩家。为了做到这一点,每个玩家都需要在拾取/投掷/携带该物体之前,使用方法
photonView.RequestOwnership()
请求该物体的所有权
还有一个完整的示例,介绍如何在光子统一软件包(DemoChangeOwner Scene.Unity)上请求所有权。感谢您的回答,我以前尝试过photonView.RequestOwner(),但没有成功,我还检查了DemoChangeOwner Scene,并将photonView.RequestOwner()放在pick()函数中,但它不起作用。你能告诉我,我应该在哪个函数中写它吗?每当玩家需要改变一个他不是所有者的对象的变换时,你应该调用它。请记住,为了更改对象的所有者,必须将OwnershipOption设置为Request或Takeover。我的游戏对象的photonView设置为Request,我还将photonView.RequestOwnership()放在carry()函数=>in Update()函数中,这是错误的,但我想测试它。但它仍然不起作用。两名玩家在试图抓住物体后都僵住了。。。。。。。。。。。。。。。。Debug.Log:Ev OwnershipRequest:{245=System.Int32[],254=1}ViewID:1001 from:1 Time:709。。。。。。。。。。。。。。。。。。Ev OwnershipRequest PhotonView.ownershipTransfer:Takeover.ownerId:1 isOwnerActive:True。此客户端的播放机:#01'{}
[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;
}
}