- Home /
Objects instantiated by the host show on all clients but client objects don't show on host.
Hello, I have this issue where if the host of the room shoots a bullet, it shows on all clients, but when a client shoots a bullet it only shows for them.
Here is my code:
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
InvokeRepeating("ShootBullet", 0, 1 / rateOfFire);
}
if(Input.GetKeyUp(KeyCode.Mouse0))
{
CancelInvoke("ShootBullet");
}
}
[Client]
void ShootBullet()
{
if(!isLocalPlayer)
{
return;
}
Vector2 mousePosition = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
Vector3 difference = new Vector3(mousePosition.x, mousePosition.y, 0) - transform.position;
difference.Normalize();
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
GameObject _bullet = Instantiate(bullet, transform.position, Quaternion.Euler(0, 0, rotZ));
//_bullet.GetComponent<Rigidbody2D>().AddForce(_bullet.transform.right * 10);
_bullet.transform.position += _bullet.transform.right * 10;
NetworkServer.Spawn(_bullet);
FindObjectOfType<AudioManager>().Play("Shoot");
}
//[Command]
//void CmdShootBullet()
//{
// InvokeRepeating("RpcShootBullet", 0, 1/rateOfFire);
//}
Any help please? I feel like it's something wrong with the paramaters ([ClientRpc], [Command], etc)
EDIT: I think I have found the issue, I have updated my code:
public GameObject bullet; public float rateOfFire = 0.10f;
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
InvokeRepeating("CmdShootBullet", 0, 1 / rateOfFire);
}
if(Input.GetKeyUp(KeyCode.Mouse0))
{
CancelInvoke("CmdShootBullet");
}
}
[Command]
void CmdShootBullet()
{
if(!isLocalPlayer)
{
return;
}
if(!NetworkServer.active)
{
Debug.LogError("PlayerShoot: No Network Server Active");
return;
}
Vector2 mousePosition = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
Vector3 difference = new Vector3(mousePosition.x, mousePosition.y, 0) - transform.position;
difference.Normalize();
float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
GameObject _bullet = Instantiate(bullet, transform.position, Quaternion.Euler(0, 0, rotZ));
NetworkServer.Spawn(_bullet);
_bullet.transform.position += _bullet.transform.right * 5.5f;
_bullet.GetComponent<Rigidbody2D>().velocity = _bullet.transform.right * _bullet.GetComponent<Bullet>().speed;
FindObjectOfType<AudioManager>().Play("Shoot");
}
I was getting an error on the non-host client saying something along the lines of "Can't spawn objects without the NetworkServer active, however I wouldn't get it on the host. If anyone can please help it would be much appreciated.
Thanks.
Sooo... I dunno a lot about this, so I'm not much more help then a rubber duck... buuuuuut.
Here are some questions I have: !isLocalPlayer Is this a stub...? Why is this here? Doesn't it make is so that the function only works for non-local players? What is the definition of a client then, if there can be a local client? And why does that matter? Wait, is the method only meant to be handling bullets shot by other players?
GameObject _bullet = Instantiate(...blah) Does this create the local bullet (from the prefab), for the client? That would explain why clients can see their own bullets.
NetworkServer.Spawn(_bullet); When a client shoots, they can only see their bullet... when the host shoots, all clients can see their bullet... but is the host not a type of client, just connecting to the local server? What is special about the host that enables other clients to see their actions?
Again, I'm just a rubber duck - I haven't really ever fiddled around with this, I'm highly likely to be asking dumb questions ¯_(ツ)_/¯ .
Your answer
Follow this Question
Related Questions
How can I spawn a GameObject on all players in my multiplayer game? 0 Answers
Level Instantiation running on Server and client 0 Answers
Instantiated object from client player not moving when spawned. How can I fix this? 0 Answers
Network.Instantiate issues - GameObject ownership and network views? 0 Answers
Having problem with making non networked objects only be intractable with local player 0 Answers