- Home /
Instantiating Rigidbodies on PhotonNetwork? Am I doing it wrong?
I've been working on a small multiplayer project with a friend, and I am attempting to create a script that will handle instantiating thrown objects, like grenades or other objects, using PhotonNetwork.Instantiate in C#. I keep receiving an error that says;
"Error CS0029: Cannot implicitly convert type 'UnityEngine.GameObject' to 'UnityEngine.Rigidbody'"
This makes sense to me, but the problem comes along when I review the code, just saying I am a bit new to C# with Unity so I need some help figuring out why this is wrong:
void throwObject () {
// throws the specified object. (Needs to be a rigidbody)
Rigidbody objectToThrow;
objectToThrow = PhotonNetwork.Instantiate( selectedObject, transform.position, transform.rotation, 0 ) ;
objectToThrow.velocity = transform.TransformDirection(Vector3.forward * throwForce);
}
Answer by Jeff00001 · Nov 30, 2014 at 04:09 PM
public Rigidbody Missile;
public float MissileForce = 5500.0f;
public AudioClip MissileSound;
void ShootMissile()
{
if(missilecooldown > 0) {
return;
}
//Debug.Log ("Firing our Missiles!");
GetComponent<PhotonView>().RPC ("MissileActive", PhotonTargets.All);
missilecooldown = missilefireRate;
audio.PlayOneShot(MissileSound);
}
[RPC]
private void MissileActive()
{
Rigidbody clone;
clone = Instantiate(Missile, Gun.position, Gun.rotation) as Rigidbody;
clone.rigidbody.AddForce(transform.forward * MissileForce);
}
Hope this example helps.
What's the reasoning for adding the force in the RPC call, as opposed to each instantiated projectile having a script to move it. - All clients would run the same thing once the instantiation is synchronized.