Respawned with Camera problem

Hello! Whenever my player respawns, my camera does not follow the player. I’m using a first person controller and the more I respawn, the further my camera is from my player and it became very hard to control… What my script does is when my player hit into some obstacles called “Crash” the life will decrease by 1 and automatically respawn. If lives = 0, level “GameOver” loads. This script is linked to my health script. Also why is it that the height of my character controller always increase to 4? Please advise. Thank you :slight_smile:

Damage script:

 var otherScript : Health;
 var nextActivationTime = 0.0;
 var applyDamageDuration = 4;
 var deathSound : UnityEngine.AudioClip;
    
private var timer = 0.0;
private var levelStateMachine : LevelStatus;		

function OnControllerColliderHit (collisionObject: ControllerColliderHit)
{ 
	if(collisionObject.gameObject.name == "Crash"  && Time.time > nextActivationTime) 
	{ 
		nextActivationTime = Time.time + applyDamageDuration;
		otherScript.lives--;
		yield WaitForSeconds(3);
		Again();


	if(otherScript.lives <= 0)
	Die();	
		
	}
		
}

function Again()
{
	if (otherScript.lives>0)
	respawnPosition = Respawn.currentRespawn.transform.position;
	Camera.main.transform.position = respawnPosition - (transform.forward * 4) + Vector3.up;	
	SendMessage("HidePlayer");
	
	transform.position = respawnPosition + Vector3.up;

	yield WaitForSeconds(1.6);	

	SendMessage("ShowPlayer");	
	Respawn.currentRespawn.FireEffect ();
}

function Die()
{
	if (deathSound)
		{
			AudioSource.PlayClipAtPoint(deathSound, transform.position);

		}

	Application.LoadLevel("GameOver");	
}

Health script:

var lives = 3;

var speedBoostDuration = 4;
var nextActivationTime = 0.0;

private var timer = 0.0;


function OnControllerColliderHit (collisionObject: ControllerColliderHit)
{ 
	if(collisionObject.gameObject.name == "Heart"  && Time.time > nextActivationTime) 
	{ 
	
		nextActivationTime = Time.time + speedBoostDuration;
		Destroy(collisionObject.gameObject); 
		
				if (lives == 3)
			
			lives += 0;
			
			else
			lives++;
			if(lives <=0)
			Application.LoadLevel("GameOver");
			yield WaitForSeconds(3);
			

		
			
		
	} 
	
	
}

Do I have to write health = maxHealth;? I saw the 3rd person tutorial and didn’t get why they did that… Thank you :slight_smile:

Hey there (-; try this
Java Script

var maximumHitPoints = 100.0;

var hitPoints = 100.0;

var bulletGUI : GUIText;

var rocketGUI : DrawRockets;

var healthGUI : GUITexture;

var walkSounds : AudioClip;

var painLittle : AudioClip;

var painBig : AudioClip;

var die : AudioClip;

var audioStepLength = 0.3;

private var machineGun : MachineGun;

private var rocketLauncher : RocketLauncher;

private var healthGUIWidth = 0.0;

private var gotHitTimer = -1.0;

var rocketTextures : Texture;

function Awake () {
machineGun = GetComponentInChildren(MachineGun);
rocketLauncher = GetComponentInChildren(RocketLauncher);

PlayStepSounds();

healthGUIWidth = healthGUI.pixelInset.width;

}

function ApplyDamage (damage : float) {
if (hitPoints < 0.0)
return;

// Apply damage
hitPoints -= damage;

// Play pain sound when getting hit - but don't play so often
if (Time.time > gotHitTimer && painBig && painLittle) {
	// Play a big pain sound
	if (hitPoints < maximumHitPoints * 0.2 || damage > 20) {
		audio.PlayOneShot(painBig, 1.0 / audio.volume);
		gotHitTimer = Time.time + Random.Range(painBig.length * 2, painBig.length * 3);
	} else {
		// Play a small pain sound
		audio.PlayOneShot(painLittle, 1.0 / audio.volume);
		gotHitTimer = Time.time + Random.Range(painLittle.length * 2, painLittle.length * 3);
	}
}

// Are we dead?
if (hitPoints < 0.0)
	Die();

}

function Die () {
if (die)
AudioSource.PlayClipAtPoint(die, transform.position);

// Disable all script behaviours (Essentially deactivating player control)
var coms : Component[] = GetComponentsInChildren(MonoBehaviour);
for (var b in coms) {
	var p : MonoBehaviour = b as MonoBehaviour;
	if (p)
		p.enabled = false;
}

LevelLoadFade.FadeAndLoadLevel(Application.loadedLevel, Color.white, 2.0);

}

function LateUpdate () {
// Update gui every frame
// We do this in late update to make sure machine guns etc. were already executed
UpdateGUI();
}

function PlayStepSounds () {
var controller : CharacterController = GetComponent(CharacterController);

while (true) {
	if (controller.isGrounded && controller.velocity.magnitude > 0.3) {
		audio.clip = walkSounds[Random.Range(0, walkSounds.length)];
		audio.Play();
		yield WaitForSeconds(audioStepLength);
	} else {
		yield;
	}
}

}

function UpdateGUI () {
// Update health gui
// The health gui is rendered using a overlay texture which is scaled down based on health
// - Calculate fraction of how much health we have left (0…1)
var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);

// - Adjust maximum pixel inset based on it
healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;

// Update machine gun gui
// Machine gun gui is simply drawn with a bullet counter text
if (machineGun) {
	bulletGUI.text = machineGun.GetBulletsLeft().ToString();
}

// Update rocket gui
// This is changed from the tutorial PDF. You need to assign the 20 Rocket textures found in the GUI/Rockets folder
// to the RocketTextures property.
if (rocketLauncher)	{
	rocketGUI.UpdateRockets(rocketLauncher.ammoCount);
	/*if (rocketTextures.Length == 0) {
		Debug.LogError ("The tutorial was changed with Unity 2.0 - You need to assign the 20 Rocket textures found in the GUI/Rockets folder to the RocketTextures property.");
	} else {
		rocketGUI.texture = rocketTextures[rocketLauncher.ammoCount];
	}*/
}

}

Which camera script are you using? If it’s some camera following script like MouseOrbit.js, you must set the variable target to the player transform when respawning. Supposing it’s MouseOrbit.js, you should add this script to your player prefab:

function Start(){
  var camScript: MouseOrbit = Camera.main.GetComponent(MouseOrbit);
  camScript.target = transform; // set variable target to the player transform
}

If your camera uses another script, replace MouseOrbit above with the script’s name without quotes or extension (it’s the script type). If you’re using other kind of camera script which doesn’t have the target variable, let me know.

EDITED: God, I must be blind: the problem is in your script, in the function Again. You don’t destroy your player, just teleport it to the respawn point. Tell me: do you use a first person style camera during the game? And during respawning, what do you want to do? Hide the player, move the camera to the respawn point, wait 1.6 seconds, then show the player and special effects, right? And what after? Return the camera to the first person style? If that’s the idea, you should save the camera’s local position, set the new local position relative to the player, move the player, do the respawning effect, then restore the local position. That’s how the function should be:

function Again()
{
    if (otherScript.lives>0){ // <- this bracket was missing!
      var camTr = Camera.main.transform; // let's cache the cam transf
      var camPos = camTr.localPosition; // save cam local pos
      respawnPosition = Respawn.currentRespawn.transform.position;
      // places the camera 4 units behind and 1 unit above player
      camTr.localPosition += Vector3.up - 4 * Vector3.forward;
      SendMessage("HidePlayer");
      // teleports the player to 1 unit above respawnPosition
      transform.position = respawnPosition + Vector3.up;
      yield WaitForSeconds(1.6); // a little suspense
      SendMessage("ShowPlayer"); // the player is back! 
      Respawn.currentRespawn.FireEffect ();
      yield WaitForSeconds(0.5); // watch the effects for a while
      camTr.localPosition = camPos; // restore cam local pos
    } // close the if bracket
}