Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by RamboPanda · Aug 02, 2011 at 01:26 AM · cameradamagerespawn

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 :)

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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
}
Comment
Add comment · Show 12 · 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
avatar image RamboPanda · Aug 02, 2011 at 02:50 AM 0
Share

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.

avatar image RamboPanda · Aug 02, 2011 at 03:40 AM 0
Share

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. :)

avatar image aldonaletto · Aug 02, 2011 at 01:42 PM 0
Share

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?

avatar image RamboPanda · Aug 03, 2011 at 04:55 AM 0
Share

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.

avatar image aldonaletto · Aug 03, 2011 at 06:11 AM 0
Share

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.

Show more comments
avatar image
0

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];
     }*/
 }

}

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

4 People are following this question.

avatar image avatar image avatar image avatar image

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


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