x


Network - Killing Player, Then Respawn - Issue with RPC

Hello,

I'm starting to get to grips with Unity's networking tools. Pretty awesome stuff... Anyway, I'm working on a kill script and a respawn script, both are very close to working!

If I am my network (so, I'm the dude getting killed):

My Kill script (attached to player) is basically watching out for my health, if it drops below 0, then initiate blood splatter and sends player.transform to spawn script (attached to GO on root).

My Spawn script then turns off the player.gameObject, waits a few seconds, changes the players position to a randomly selected spawn point, turns the gameObject back on, restores health.

My kill script also initiates an RPC, but then is when it goes a little weird... I keep changing my script around, but whats happening right now. I, the player spawns, in a randomly selected point... as do my clients, but they spawn in a different place according to their player. In other words, I die, I spawn top left of map, but the person who killed me (client) sees me spawn bottom right of map.

I think its to do with my randomly selecting spawn point (spawn = Random.Range(0, spawnPoints.length);)

I hope your still with me, here is my HealthSystem (Kill - attached to player):

function Update(){
// -- We are dead -- \\
    if(currentHealthPercent < 1 || currentHealth < 1){
       alive = false;
    }

    if(alive == false){
       if(networkView.isMine){

         //kill us (turn off gameObject)
         KillPlayer(transform);

         //tell clients to turn off gameObject too
         var viewID = Network.AllocateViewID();
         networkView.RPC("SendKillPlayer", RPCMode.All, viewID);

         //alive = true;
       }
    }
}

function KillPlayer(player : Transform){

    print("You Died...");

    //blood splatter
    var randomRotate = Random.Range(0,360);
    var clone : Transform;
    clone = Network.Instantiate(bloodSplatter, Vector3(transform.position.x,0.01,transform.position.z), transform.rotation, 0);

    //randomly respawn player
    for (var go in FindObjectsOfType(GameObject)){
       go.SendMessage("RespawnPlayer", player, SendMessageOptions.DontRequireReceiver);
       print("Respawning Player...");
    }
}


@RPC
function SendKillPlayer(viewID : NetworkViewID){

    for (var go in FindObjectsOfType(GameObject)){
       go.SendMessage("RespawnPlayer_CLIENT", transform, SendMessageOptions.DontRequireReceiver);
       print("Respawning Player CLIENT...");
    }
}

And here is my SpawnPrefab Script (Spawn - attached to root hierarchy)

var spawn : int;

function RespawnPlayer(character : Transform){
    //SET UP RANDOM SPAWN DIGIT
    spawn = Random.Range(0, spawnPoints.length);

    //SELF
    RespawnPlayer_SELF(character, spawn);

    //CLENTS
    //var viewID = Network.AllocateViewID();
    //networkView.RPC("RespawnPlayer_CLIENT", RPCMode.All, viewID, character);

} 

function RespawnPlayer_SELF(player : Transform, spawn : int){

    print("** Respawn Active! **");

    //turn off player << LOCALLY
    player.gameObject.SetActiveRecursively(false);

    //wait a few seconds
    yield WaitForSeconds(3);

    //randomly respawn player
    player.transform.position = spawnPoints[spawn].transform.position;

    print("Reinitiating Player...");

    //restore health
    health = player.gameObject.GetComponent(HealthSystem);
    health.currentHealth = health.maxHealth;

    //set health controller's alive back to true
    health.alive = true;
}

@RPC
function RespawnPlayer_CLIENT(client : Transform){ //, spawn : int){  viewID : NetworkViewID

    print("******* RPC ACTIVE - CLIENT *******");

    //turn off player << CLIENT
    client.gameObject.SetActiveRecursively(false);

    //wait a few seconds
    yield WaitForSeconds(3); //extra time (incase of lag)

    //PLAYER'S SPAWN POINT ALREADY SELECTED, spawn in clients
    client.transform.position = spawnPoints[spawn].transform.position;

    //restore health < CLIENT (SHOULD BE EQUAL TO PLAYER)
    health = client.gameObject.GetComponent(HealthSystem);
    health.currentHealth = health.maxHealth;

    //turn players renderer on, but deactivate rendererd guns, AND direction arrows < CLIENT
    client.gameObject.SetActiveRecursively(true);
    var weapons = client.gameObject.GetComponent(WeaponController);
    weapons.changeWeapon(weapons.currentWeapon);

    var character = client.gameObject.GetComponent(CharacterMove);
    character.playerArrow.gameObject.SetActiveRecursively(false);
    character.enemyArrow.gameObject.SetActiveRecursively(false);
}

... Well thats a mouth full. I think, what I need to do, is have the Random value that selects my random spawn point, the same for local and clients, but I have no idea how to pass on those variables with the method I'm using!

more ▼

asked May 23 '12 at 02:21 PM

oliver-jones gravatar image

oliver-jones
2.5k 205 225 254

(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x709
x572
x440
x237
x63

asked: May 23 '12 at 02:21 PM

Seen: 627 times

Last Updated: May 23 '12 at 08:30 PM