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 /
This question was closed Dec 27, 2016 at 07:39 PM by wesleywh for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by wesleywh · Mar 07, 2015 at 11:28 PM · cameramultiplayerinstantiation

Camera Switching When Player Dies

So I can't seem to figure this one out at all. I'm not sure if I'm explaining this well enough, if so let me know. Player 1 shoots player 2 and player 2's health reaches 0. When that happens player 2's character is destroyed and replaced by a rag doll dead character with a new camera attached to it. Player 2 does a seamless switch from his first camera to the second camera just fine. However, when anyone kills player 2 everyones camera's in the game is switched to that players view and I simply can't figure this one out.

I have a camera attached to the head of the rag doll with a network view watching it. I also have a network view watching the following script:

 #pragma strict
 
 function Start () {
     if(!networkView.isMine){
         if(this.GetComponent(Camera)){
             this.GetComponent(Camera).camera.enabled = false;
         }
         if(this.GetComponent(MouseLookPlus)){
             this.GetComponent(MouseLookPlus).enabled = false;
         }
     }
 }

This is what I have only the player to check when to spawn the rag doll when they are dead:

 @RPC
 function Replace() {
  
     // If we have a dead barrel then replace ourselves with it!
     // Destroy ourselves
     Counter.SpawnedEnemies -= 1;
     Network.Destroy(this.gameObject);
     if (deadReplacement) {
         var dead : Rigidbody = Network.Instantiate(deadReplacement, GOPos.transform.position, GOPos.transform.rotation, 1);
         //scoreManager.addScore(20);
         // For better effect we assign the same velocity to the exploded barrel
         dead.rigidbody.velocity = rigidbody.velocity;
         dead.angularVelocity = rigidbody.angularVelocity;
     }
 }
 @RPC
 function ApplyDamage (damage : int) {
     if (hitPoints <= 0.0)
         return;
 
     hitPoints -= damage;
     if (hitPoints <= 0.0){
         networkView.RPC("Replace", RPCMode.AllBuffered);
         }
 }


Finally I have this attached to the gun that is sending the damage to the player.

 var direction = gameObject.transform.TransformDirection(Vector3(Random.Range(-0.01, 0.01) * triggerTime, Random.Range(-0.01, 0.01) * triggerTime,1));
     var hit : RaycastHit;
     var position = transform.parent.position;
 
     if (Physics.Raycast(position, direction, hit, range, layerMask.value)) {
 if (hit.transform.tag == "Enemy" || hit.transform.tag == "Zombie" || hit.transform.tag == "HumanEnemy") {
             hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
             //hit.transform.root.networkView.RPC("ApplyDamage",  RPCMode.AllBuffered, damage);
             yield WaitForSeconds(0.01);
             var bloodHole = Instantiate (Blood, contact, rotation) as GameObject;
             if(Physics.Raycast (position, direction, hit, range, layerMask.value)){
                 if(hit.rigidbody){
                     hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
                 }
             }    
         }


Help of any kind on this error that I am having would be greatly appreciated.

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 wesleywh · Mar 21, 2015 at 10:21 PM 0
Share

Just bumping this. I still haven't figured this one out and it has been a while.

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by wesleywh · Mar 21, 2015 at 10:41 PM

So i found out the problem and fixed it on my own. The issue was with the following script (this script is wrong):

 @RPC
  function Replace() {
   
      // If we have a dead barrel then replace ourselves with it!
      // Destroy ourselves
      Counter.SpawnedEnemies -= 1;
      Network.Destroy(this.gameObject);
      if (deadReplacement) {
          var dead : Rigidbody = Network.Instantiate(deadReplacement, GOPos.transform.position, GOPos.transform.rotation, 1);
          //scoreManager.addScore(20);
          // For better effect we assign the same velocity to the exploded barrel
          dead.rigidbody.velocity = rigidbody.velocity;
          dead.angularVelocity = rigidbody.angularVelocity;
      }
  }
  @RPC
  function ApplyDamage (damage : int) {
      if (hitPoints <= 0.0)
          return;
  
      hitPoints -= damage;
      if (hitPoints <= 0.0){
          networkView.RPC("Replace", RPCMode.AllBuffered);
          }
  }

What it should have been was the following:

  function Replace() {
      if(this.GetComponent(NetworkView).isMine){//Notice I added a network ownership check here
      // If we have a dead barrel then replace ourselves with it!
      // Destroy ourselves
      Counter.SpawnedEnemies -= 1;
      if (deadReplacement) {
          var dead : Rigidbody = Network.Instantiate(deadReplacement, GOPos.transform.position, GOPos.transform.rotation, 1);
           Network.Destroy(this.gameObject);//notice this is now after instantiation
          //scoreManager.addScore(20);
          // For better effect we assign the same velocity to the exploded barrel
          dead.rigidbody.velocity = rigidbody.velocity;
          dead.angularVelocity = rigidbody.angularVelocity;
      }
      }
  }
  @RPC
  function ApplyDamage (damage : int) {
      if (hitPoints <= 0.0)
          return;
  
      hitPoints -= damage;
      if (hitPoints <= 0.0){
          Replace(); //Notice this is no longer an RPC call
          }
  }

I commented the things that I changed. Hopefully this will help someone else that is having the same issue. I was basically having everyone trying to Network.Instantiate and I was destroyed the gameobject doing the instantiation call prior to instantiating anything. This caused strange behavior to occur as I stated in my original question.

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

Follow this Question

Answers Answers and Comments

21 People are following this question.

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

Related Questions

TPS camera in multiplayer... 2 Answers

Have Camera Render First Person Arms But Not Body. 2 Answers

2 players 1 game field in shooter/card game 0 Answers

Multiplayer click and spawn 1 Answer

Multiplayer camera please help 2 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