0
using UnityEngine;
using System.Collections;
public class SpawnGameObjectsNetwork : Photon.MonoBehaviour
{
public float secondsBetweenSpawning = 0.1f;
public float xMinRange = -25.0f;
public float xMaxRange = 25.0f;
public float yMinRange = 8.0f;
public float yMaxRange = 25.0f;
public float zMinRange = -25.0f;
public float zMaxRange = 25.0f;
public float zMinRange2 = 81.0f;
public float zMaxRange2 = 85.0f;
public string spawnObjects;
Vector3 spawnPosition;
Vector3 spawnPosition2;
public float nextSpawnTime;
void Start()
{
nextSpawnTime = Time.time+secondsBetweenSpawning;
}
void Update()
{
if (PhotonNetwork.isMasterClient)
{
if (Time.time > nextSpawnTime)
{
Vector3 spawnPosition;
Vector3 spawnPosition2;
spawnPosition.x = Random.Range (xMinRange, xMaxRange);
spawnPosition.y = Random.Range (yMinRange, yMaxRange);
spawnPosition.z = Random.Range (zMinRange, zMaxRange);
spawnPosition2.x = Random.Range (xMinRange, xMaxRange);
spawnPosition2.y = Random.Range (yMinRange, yMaxRange);
spawnPosition2.z = Random.Range (zMinRange2, zMaxRange2);
GetComponent<PhotonView>().RPC("MakeThingToSpawn", PhotonTargets.All, spawnPosition, spawnPosition2);
nextSpawnTime = Time.time + secondsBetweenSpawning;
}
}
}
[PunRPC]
void MakeThingToSpawn(Vector3 spawnPosition, Vector3 spawnPosition2)
{
GameObject spawnedObject = PhotonNetwork.Instantiate(spawnObjects, spawnPosition, transform.rotation, 0) as GameObject;
GameObject spawnedObject2 = PhotonNetwork.Instantiate(spawnObjects, spawnPosition2, transform.rotation, 0) as GameObject;
}
}
此代碼連接到播放器預製件,並創建2個對象,在 同一地點,至極的所有玩家都可見。一個玩家可以移動 其中一個對象,並且不能控制另一個,並且 與第二個玩家相反。如何做到這一點只有一個實例化對象,可以控制(所有玩家都可以使用 另一個腳本移動)?如何在Photon Unity Network中正確實例化遊戲對象?
謝謝您的回答,可惜的i've已經嘗試PhotonNetwork.InstantiateSceneObject(),和transferingOwnership,但它didn't解決我的問題,我覺得現在的問題很可能是在我的「抓取對象」腳本中。 –