Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by oliver-jones · May 23, 2012 at 02:21 PM · networkrandomspawnrpckill

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Griffo · Oct 20, 2013 at 06:00 PM 0
Share

This might be an old post but +1 for helping me out ..

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by nastasache · Oct 20, 2013 at 10:25 PM

I can't help you too much, I can only share a part of code (mainly, the original source is TPS Unity3D tutorial) taking care about spawning on rand points (and working for me as expected), maybe you can get a bit a direction:

 private NetworkPlayer localPlayer;
 private NetworkViewID localTransformViewID;
 private bool isInstantiated = false;
 
 public void OnPlayerConnected (NetworkPlayer playerNID) {
     NetworkViewID transformViewID = Network.AllocateViewID();
     networkView.RPC("InitPlayer", playerNID, playerNID, transformViewID);
     
 }    
 
 [RPC]
 void InitPlayer (NetworkPlayer playerNID, NetworkViewID transformViewID ) {
     localPlayer = playerNID;
     localTransformViewID = transformViewID;
 }    
 
 void Update() {
 
     if (Network.isClient  && !isInstantiated) {
     
         GameObject[] spawnPointsInScene = GameObject.FindGameObjectsWithTag("SpawnPoint");
         
         int idx = Random.Range(0,spawnPointsInScene.Length);
         
         currentSpawnPoint = spawnPointsInScene[idx].transform.position;
         
         networkView.RPC("SpawnPlayer", RPCMode.AllBuffered, localPlayer, localTransformViewID, currentSpawnPoint);
     
         isInstantiated = true;
     }
 }
 
 [RPC]
 void SpawnPlayer (NetworkPlayer playerNID, NetworkViewID transformViewID, Vector3 currentSpawnPoint) {
 
     instantiatedPlayer = Instantiate(playerPrefab, currentSpawnPoint, transform.rotation) as Transform;
     
 }
 

Probably, depending by your other rules in networking, it's about RPCMode and Network.isClient/Network.isServer (or networkView.isMine) to not take rand() as different on each client when a client ask for instantiating.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Network initializing player 1 Answer

Generate gameObject horde, more positioned towards front of horde 1 Answer

Small RPC Code Not Working 1 Answer

RPC function to update clients varialbe 0 Answers

Mutliplayer SetActive(true) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges