- Home /
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 :)
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 :)
Answer by aldonaletto · Aug 02, 2011 at 02:24 AM
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 }
Okay I have added $$anonymous$$ouseOrbit to the main camera and the above script to the player. But the camera is really far from the player. I tried to change the above script to $$anonymous$$ouseLook but after respawning, I only could control the camera and it didn't follow the player. The $$anonymous$$ouseLook script doesn't have a target variable.
I managed to reduce the distance by changing the distance in the $$anonymous$$ouseOrbit script to 0 but is it possible to shift it to eye level? It seems to work fine now except that I need to shift it up. I tried to manually place it at eye level but when I test it out the camera will move down to the belly area and the controls got out of control (left became right, going forward became going backward)... What should I do? Thank you for your help. :)
How the camera was set in your previous version? Usually the $$anonymous$$ain Camera is a Player's child, so it goes everywhere the player goes - and if the player dies, it respawns with a new camera, since the camera is part of the player prefab. Did you use the standard First Person Controller from Standard Assets?
Hello. Oh I see... Yes I'm using the standard first person controller from the standard assets. All I did was changed the name to Player.
The First Person Controller comes with the camera childed to it. I suspect you've screwed your player prefab somehow, and that's why the player respawns with the camera at strange positions - it also explains why your character reborns with height 4 (this may have been changed by mistake in the current player prefab).
$$anonymous$$ake sure your player is ok, then drag it to your player prefab - this will assure your player reborns always the same. There's no trouble if your player prefab is the First Person Controller prefab; this will affect only the prefab in this project.
Answer by BriceMcphailTV · Aug 02, 2011 at 01:34 AM
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];
}*/
}
}
Your answer
Follow this Question
Related Questions
How to create a fade out respawn from falling? 1 Answer
How can I re-spawn a script component that has been destroyed? 2 Answers
How to make other objects target an instantiated object? 0 Answers
Player taking damage on collision. Can't get the script to work!? 1 Answer
Programming A Reset Scene 0 Answers