[英]Unity Mirror Networking Object not move on client
i´m work on a networked game and stuck on an problem with the instantiation or NetworkSpawn of an bullet.我正在开发一款网络游戏,但遇到了子弹的实例化或 NetworkSpawn 的问题。 If the hostplayer/server "fires" the bullet is spaned, synced and flys all the way it should do.如果主机播放器/服务器“开火”,子弹就会跨越、同步并按其应有的方式飞行。 If a client "fires" the bullet will be spawned but stays on one point without any velocity.如果客户端“开火”,子弹将被产生但停留在一个点上,没有任何速度。
Server/Host fires -> everythings fine.服务器/主机触发 -> 一切正常。
Client fires -> bullet spawned but does not move.客户端开火 -> 子弹产生但不移动。
Following i´ll show you a part of the script:下面我将向您展示脚本的一部分:
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);
}
Thank you for your effort!感谢你的付出! :-) :-)
You need to call a function on client from the server.您需要从服务器调用客户端上的函数。 Use the:使用:
[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);
}
And call it from the:并从以下位置调用它:
[Command]
void CmdFire()
{
RpcFireOnClient();
}
I hope it helps, i am learning it right now and do not know if it is totally true.我希望它有所帮助,我现在正在学习它,不知道它是否完全正确。 But give it a try!但是试一试吧!
Cheers干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.