我做了一个简单的多人射击游戏,到目前为止一切都很好,除了所选择的武器没有显示enyone但主机例如如果我选择武器1(ak47)没有人可以看到除了服务器主机相同的对象为我的其他武器,我相信我需要使用NetworkServer.Spawn生成它们,但我不知道该怎么做。谢谢eny anwsers !!!
using UnityEngine;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class PlayerController : NetworkBehaviour
{
public Text Ammoleft;
[SyncVar]
public int Ak47Bullets = 30;
[SyncVar]
public int PipeShotgunBullets = 7;
[SyncVar]
public int currentWeapon;
public Transform[] weapons;
[SyncVar]
public string CurrentWeaponstr;
public GameObject ShottyBullet;
public GameObject Ak47Bullet;
public Transform bulletSpawn;
public float speed = 1; // speed in meters per second
void Update()
{
if (!isLocalPlayer)
{
return;
}
float mouseInput = Input.GetAxis("Mouse X");
Vector3 lookhere = new Vector3(0,mouseInput,0);
transform.Rotate(lookhere);
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 3.0f;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;
transform.Translate(x, 0, 0);
transform.Translate(0, 0, z);
Vector3 moveDir = Vector3.zero;
//moveDir.x = Input.GetAxis("Horizontal"); // get result of AD keys in X
//moveDir.z = Input.GetAxis("Vertical"); // get result of WS keys in Z
// move this object at frame rate independent speed:
transform.position += moveDir * speed * Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Alpha1))
{
CmdSwitchtoAk();
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
CmdSwitchtoPipe();
}
if (Input.GetKeyDown (KeyCode.Mouse0) && CurrentWeaponstr ==
"PipeShotgun" && PipeShotgunBullets > 0)
{
CmdFireShotty();
PipeShotgunBullets -= 1;
Ammoleft.text = "Left: " + PipeShotgunBullets;
Debug.Log ("Shotpipe");
}
if (Input.GetKey (KeyCode.Mouse0) && CurrentWeaponstr == "Ak47" &&
Ak47Bullets > 0) {
CmdFireAk47();
Ak47Bullets -= 1;
Ammoleft.text = "Left: " + Ak47Bullets;
Debug.Log ("ShotAk");
}
}
[Command]
void CmdSwitchtoAk()
{
changeWeapon(1);
CurrentWeaponstr = "Ak47";
}
[Command]
void CmdSwitchtoPipe()
{
changeWeapon(2);
CurrentWeaponstr = "PipeShotgun";
}
// This [Command] code is called on the Client …
// … but it is run on the Server!
[Command]
void CmdFireAk47(){
if (CurrentWeaponstr == "Ak47") {
var Ak47bullet = (GameObject)Instantiate (
Ak47Bullet,
bulletSpawn.position,
bulletSpawn.rotation);
Ak47bullet.GetComponent<Rigidbody>().velocity = Ak47bullet.transform.forward * 6;
NetworkServer.Spawn(Ak47bullet);
}
}
[Command]
void CmdFireShotty()
{
// Create the Bullet from the Bullet Prefab
if (CurrentWeaponstr == "PipeShotgun") {
var Pipebullet = (GameObject)Instantiate (
ShottyBullet,
bulletSpawn.position,
bulletSpawn.rotation);
Pipebullet.GetComponent<Rigidbody>().velocity = Pipebullet.transform.forward * 6;
NetworkServer.Spawn(Pipebullet);
}
}
public override void OnStartLocalPlayer ()
{
GetComponent<MeshRenderer>().material.color = Color.blue;
}
public void changeWeapon(int num) {
currentWeapon = num;
for(int i = 0; i < weapons.Length; i++) {
if (i == num) {
weapons [i].gameObject.SetActive (true);
}
else
weapons[i].gameObject.SetActive(false);
}
}
}
这是图像:
答案 0 :(得分:1)
你正在调用一个Command,这就是为什么它只在服务器/主机上运行,命令是服务器代码,RPC是客户端代码,所以如果你想向你需要的每个人发送一些动作从服务器/主机调用RPC。
public class PlayerController : NetworkBehaviour
{
[Command]
void CmdSwitchtoAk()
{
RpcSwitchtoAk();
}
[ClientRpc]
void RpcSwitchtoAk()
{
changeWeapon(1);
CurrentWeaponstr = "Ak47";
}
}