Spawn Gameobject over all Clients and server not working
Hey guys i am trying to get my photon view to instantiate my bullet over the photon network but i dont know how to Get this rigidbody bullet to spwan over the network when my rpc is called
[PunRPC] public void Spartan_Pistol_Fire() { {
int state = Random.Range(0, 2);
if (state == 1)
MuzzleFlash.GetComponent<MeshRenderer>().enabled = false;
else
MuzzleFlash.GetComponent<MeshRenderer>().enabled = true;
// Invoke("Fire1", m_TimeOut);
Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
}
if (isShooting == false)
{
MuzzleFlash.GetComponent<MeshRenderer>().enabled = false;
}
Hi,
this seems to be okay so far. Can you confirm that the function gets called on each client who is connected to the same room? What parameters do you use when invoking the RPC call on the PhotonView component?
Hi i am using this . im not shure if this is what you mean.
if (Input.GetButtonDown("Fire1"))
photonView.RPC("Spartan_Pistol_Fire", PhotonTargets.All);
at my end of the game it fires my gun. but on the other clienets nothing happend
by the way here is the full script
public float speed = 200;
public float fireRate = 1;
public bool isShooting = false;
public float flickerRate = .3f;
Vector3 realPosition = Vector3.zero;
Quaternion realRotation = Quaternion.identity;
void Start()
{
if (isShooting == false)
$$anonymous$$uzzleFlash.GetComponent<$$anonymous$$eshRenderer>().enabled = false;
InvokeRepeating("$$anonymous$$uzzleFlashRender", .1f, flickerRate);
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
photonView.RPC("Spartan_Pistol_Fire", PhotonTargets.All);
// Send$$anonymous$$essage("Fire1");
//
}
void $$anonymous$$uzzleFlashRender ()
{
int state = Random.Range(0, 2);
if (state == 1)
$$anonymous$$uzzleFlash.GetComponent<$$anonymous$$eshRenderer>().enabled = false;
else
$$anonymous$$uzzleFlash.GetComponent<$$anonymous$$eshRenderer>().enabled = true;
}
[PunRPC]
public void Spartan_Pistol_Fire()
{
{
int state = Random.Range(0, 2);
if (state == 1)
$$anonymous$$uzzleFlash.GetComponent<$$anonymous$$eshRenderer>().enabled = false;
else
$$anonymous$$uzzleFlash.GetComponent<$$anonymous$$eshRenderer>().enabled = true;
// Invoke("Fire1", m_TimeOut);
GameObject clone = (GameObject)PhotonNetwork.Instantiate("Bullet", transform.position, transform.rotation);
clone.GetComponent<Rigidbody>().velocity = Vector3.forward * speed;
// Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
//instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
}
if (isShooting == false)
{
$$anonymous$$uzzleFlash.GetComponent<$$anonymous$$eshRenderer>().enabled = false;
}
}
}
You should add a if (!photonView.is$$anonymous$$ine) { return; }
at the beginning of the Update function to make sure, that only the owner of the object is allowed to fire the weapon.
In the RPC function itself you have a Random.Range call. Please note that this call can have different results on each client. In your case you might have the result, that one client enables the $$anonymous$$eshRenderer but nobody else. So your game might run out of synchronization here.
Besides that instantiating the bullet should work. If it isn't visible after checking and applying above mentioned fixes, please try Debug.Log on the remote client and check if the RPC is executed at all.
Your answer
Follow this Question
Related Questions
Photon Weapon View 1 Answer
Photon Player Weapon View Error Help Please 1 Answer
Instatiate help 1 Answer
Photon Weapon synchronize View Help Please 0 Answers
Network Rigidbodies act weird? 0 Answers