I have a network game with multiple players. Each player can instantiate a block of C4 independently. I want to use “Alt Fire” to detonate the C4. The problem is that if each player drops a C4 and ONE of the players detonates, all the C4s explode. How do I make an object instantiated by a player be owned only by that player? I hope this makes sense. I have tried making it local and not much success. Thanks for the help guys.
Each NetworkView have a NetworkViewID which saves the owner of this networkview. You can use:
- if (networkView.isMine)
- if (networkView.viewID.isMine)
or do the comparison yourself but that’s what isMine is actually doing
- if (networkView.owner == Network.player)
- if (networkView.viewID.owner == Network.player)
That way the code is only executed on the owners PC.
edit
- First case “AltFire is triggered in the player script”:
If you can only place one C4 package a simple variable in your player class would be enough:
var placedC4 : GameObject = null;
function Fire()
{
[...]
var instantiatedProjectile : Rigidbody = Network.Instantiate (projectile, transform.position, transform.rotation, 0) as Rigidbody;
placedC4 = instantiatedProjectile.gameObject;
[...]
In your AltFire script you can use this variable to access the placed C4. Note: every player have it’s own variable so there’s no need to check who’s the owner in this case.
function AltFire()
{
if (placedC4 != null)
{
var instantiatedProjectile : Rigidbody = Network.Instantiate (explosion , placedC4.transform.position, placedC4.transform.rotation, 0) as Rigidbody;
Network.Destroy(placedC4);
placedC4 = null;
}
}
- Second case “AltFire is part of a script that is attached to the C4 GameObject”:
In this case you don’t have to alter anything in your Player script. Only the script on the C4 which should look like:
function AltFire()
{
if (networkView.isMine)
{
var instantiatedProjectile : Rigidbody = Network.Instantiate (explosion , transform.position, transform.rotation, 0) as Rigidbody;
Network.Destroy(gameObject);
}
}
When any player presses the detonate button (which is checked in the script on the C4) only the C4 objects that are owned by this player will react to this event.
The solution, is to attach to the C4 when instantiated a variable that is the owner’s Network Player id (using Network.player : http://unity3d.com/support/documentation/ScriptReference/Network-player.html).
Then, add this data to your explosion function and make only explode the C4 of the good player.
Hope it helps !
Here is the code that drops the C4 and then detonates it. Where should I put the code to select it as isMine? Thanks for all the help you guys are awsome!
function Fire () {
// Did the time exceed the reload time?
if (Time.time > reloadTime + lastShot && bulletsLeft > 0) {
// create a new projectile, use the same position and rotation as the Launcher.
var instantiatedProjectile : Rigidbody = Network.Instantiate (projectile, transform.position, transform.rotation, 0) as Rigidbody;
//newObjectsNetworkview.RPC("SetPlayer", RPCMode.AllBuffered, newPlayer);
//animation.Play(shootAnimation);
//gameObject.AddComponent ("NetworkView");
//var myNewTrans : Transform = Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0) as Transform;
//var newObjectsNetworkview : NetworkView = instantiatedProjectile.networkView;
// Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));
// Ignore collisions between the missile and the character controller
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
lastShot = Time.time;
bulletsLeft--;
GetBulletsLeft();
if (bulletsLeft == 0){
Reload();
}
}
}
function AltFire()
{
var theExplosion = GameObject.Find("C4(Clone)");
var instantiatedProjectile : Rigidbody = Network.Instantiate (explosion , theExplosion.transform.position, theExplosion.transform.rotation, 0) as Rigidbody;
Network.Destroy(theExplosion);
}