我在一个网络游戏上工作,遇到了一个项目符号的实例化或NetworkSpawn问题。如果主机播放器/服务器";火灾";子弹被弹开、同步并以它应该做的方式飞行;火灾";子弹将产生,但停留在一个没有任何速度的点上。
服务器/主机启动->一切都很好。
客户端激发->子弹繁殖但不移动。
下面我将向您展示脚本的一部分:
public bool shootableAngle = false;
public float bulletSpeed = 6.0f;
public GameObject bulletPrefab;
public float bulletRangeTime;
private void Update()
{
if (!isLocalPlayer)
{
return;
}
if (Input.GetMouseButtonDown(0) && shootableAngle)
{
CmdFire();
}
}
[Command]
void CmdFire()
{
//Instantiate bullet
GameObject bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
//Look for crosshair child and set direction
var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction = (transform.GetChild(0).gameObject.transform.position - transform.position).normalized;
//Give some velocity
bullet.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
//Spawn over Network
NetworkServer.Spawn(bullet);
//Destroy after given time
Destroy(bullet, bulletRangeTime);
}
感谢您的努力!:-(
您需要从服务器调用客户端上的函数。使用:
[ClientRpc]
RpcFireOnClient(){
//Instantiate bullet
GameObject bullet = Instantiate(bulletPrefab, transform.position,
transform.rotation);
//Look for crosshair child and set direction
var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 direction =
(transform.GetChild(0).gameObject.transform.position -
transform.position).normalized;
//Give some velocity
bullet.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
//Spawn over Network
NetworkServer.Spawn(bullet);
//Destroy after given time
Destroy(bullet, bulletRangeTime);
}
从中调用
[Command]
void CmdFire()
{
RpcFireOnClient();
}
我希望这有帮助,我现在正在学习,不知道这是否完全是真的。但是试试看!
干杯