- Home /
The question is answered, right answer was accepted
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.
Just bumping this. I still haven't figured this one out and it has been a while.
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.
Follow this Question
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