Search Unity

LoadLevel-Fps tutorial

Discussion in 'Editor & General Support' started by AaronC, Dec 10, 2006.

  1. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Okay,
    So I find the FPS Player scripts a bit hard to follow regarding loading levels.I like the way the new tutorial fades to white on player die, But Im dammed if i can get it to load the right level. Heres the new loading bits...
    Code (csharp):
    1.  
    2. LevelLoadFade.FadeAndLoadLevel(Application.loadedLevel, Color.white, 2.0);
    Which obviously loads the current level. Id be keen for this to work:
    Code (csharp):
    1. LevelLoadFade.FadeAndLoadLevel(Application.LoadLevel("GameOver"), Color.white, 2.0);
    But thats not gonna work. I couldnt find any references for any of this:LevelLoadFade.FadeAndLoadLevel(Application.loadedLevel

    Yo any help here would be grand. Currently Unity crashes when in game using my code...And Ive bug reported it...Thanks guys

    EDIT
    Ummm, okay, got it now, found the script in misc scripts of newFPS Tut
    No worries
    Yeah I talk my problems out loud in real life too, and then I generally find the answers.

    AC :oops: :oops:
     
  2. WarpedAxiom

    WarpedAxiom

    Joined:
    Oct 27, 2006
    Posts:
    245
    I found it too, but I still can't make it work. How did you do it?
     
  3. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    I tell a lie. I got it to compile, but no it doesnt work. fades then crashes completely. Bummer ay?
    Luckily theres access to the editmodescene file to catch up on unsaved work, as repeated crashes can be devastating. do a search for a thread called autosave if you're not sure what I mean.

    What happens to yours? complete application shutdown? Same thing with standalone builds. I think Joe wrote the code. For now we could bug him with a stream of bug reports...? :p

    Hopefully we'll be enlightened soon.
    AC
     
  4. WarpedAxiom

    WarpedAxiom

    Joined:
    Oct 27, 2006
    Posts:
    245
    I suppose the script needs to be called. I called it from another script, but Unity still claims it doesn't know what to do.
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    I just tried it in another project, with a simple script:
    Code (csharp):
    1.  
    2. function Start ()
    3. {
    4.     LevelLoadFade.FadeAndLoadLevel("grass", Color.white, 2.0);
    5. }
    6.  
    I have tried it on the fps tutorial and the grass demo project which worked fine. How exactly are you trying to use it?
     
  6. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Heres one that fades, ignores the yield statment and crashes Unity completely. My level is in build settings.


    Code (csharp):
    1. function OnTriggerEnter( Col : Collider)
    2.  
    3. {  yield WaitForSeconds(36);
    4.     LevelLoadFade.FadeAndLoadLevel("GameOver", Color.black, 30.0);
    5. }

    And heres my fps playerscript, which also fades, and completely crashes Unity.
    Code (csharp):
    1.  
    2. var maximumHitPoints = 100.0;
    3. var hitPoints = 100.0;
    4.  
    5. var bulletGUI : GUIText;
    6. var rocketGUI : GUITexture;
    7. var healthGUI : GUITexture;
    8.  
    9. var walkSounds : AudioClip[];
    10. var painLittle : AudioClip;
    11. var painBig : AudioClip;
    12. var die : AudioClip;
    13. var audioStepLength = 0.3;
    14.  
    15. private var machineGun : MachineGun;
    16. private var rocketLauncher : RocketLauncher;
    17. private var healthGUIWidth = 0.0;
    18. private var gotHitTimer = -1.0;
    19.  
    20. function Start ()
    21. {
    22.     machineGun = GetComponentInChildren(MachineGun);
    23.     rocketLauncher = GetComponentInChildren(RocketLauncher);
    24.    
    25.     PlayStepSounds();
    26.  
    27.     healthGUIWidth = healthGUI.pixelInset.width;
    28. }
    29.  
    30. function ApplyDamage (damage : float) {
    31.     if (hitPoints < 0.0)
    32.         return;
    33.  
    34.     // Apply damage
    35.     hitPoints -= damage;
    36.  
    37.     // Play pain sound when getting hit - but don't play so often
    38.     if (Time.time > gotHitTimer  painBig  painLittle)
    39.     {
    40.         // Play a big pain sound
    41.         if (hitPoints < maximumHitPoints * 0.2 || damage > 20)
    42.         {
    43.             audio.PlayOneShot(painBig, 1.0 / audio.volume);
    44.             gotHitTimer = Time.time + Random.Range(painBig.length * 2, painBig.length * 3);
    45.         }
    46.         // Play a small pain sound
    47.         else
    48.         {
    49.             audio.PlayOneShot(painLittle, 1.0 / audio.volume);
    50.             gotHitTimer = Time.time + Random.Range(painLittle.length * 2, painLittle.length * 3);
    51.         }
    52.     }
    53.  
    54.     // Are we dead?
    55.     if (hitPoints < 0.0)
    56.         Die();
    57. }
    58.  
    59. function Die ()
    60. {
    61.     if (die)
    62.         AudioSource.PlayClipAtPoint(die, transform.position);
    63.    
    64.     // Disable all script behaviours (Essentially deactivating player control)
    65.     var coms : Component[] = GetComponentsInChildren(MonoBehaviour);
    66.     for (var b in coms)
    67.     {
    68.         var p : MonoBehaviour = b as MonoBehaviour;
    69.         if (p)
    70.             p.enabled = false;
    71.     }
    72.  
    73.     LevelLoadFade.FadeAndLoadLevel("GameOver", Color.red, 2.0);
    74. }
    75.  
    76. function LateUpdate ()
    77. {
    78.     // Update gui every frame
    79.     // We do this in late update to make sure machine guns etc. were already executed
    80.     UpdateGUI();
    81. }
    82.  
    83. function PlayStepSounds ()
    84. {
    85.     var controller : CharacterController = GetComponent(CharacterController);
    86.  
    87.     while (true)
    88.     {
    89.         if (controller.isGrounded  controller.velocity.magnitude > 0.3)
    90.         {
    91.             audio.clip = walkSounds[Random.Range(0, walkSounds.length)];
    92.             audio.Play();
    93.             yield WaitForSeconds(audioStepLength);
    94.         }
    95.         else
    96.         {
    97.             yield;
    98.         }
    99.     }
    100. }
    101.  
    102.  
    103. function UpdateGUI ()
    104. {
    105.     // Update health gui
    106.     // The health gui is rendered using a overlay texture which is scaled down based on health
    107.     // - Calculate fraction of how much health we have left (0...1)
    108.     var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);
    109.     // - Adjust maximum pixel inset based on it
    110.     healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
    111.  
    112.     // Update machine gun gui
    113.     // Machine gun gui is simply drawn with a bullet counter text
    114.     if (machineGun)
    115.     {
    116.         bulletGUI.text = machineGun.GetBulletsLeft().ToString();
    117.     }
    118.    
    119.     // Update rocket gui
    120.     // We use a quicktime movie with 20 frames to display how many are left
    121.     // The alpha of the movie changes every frame thus rockets get masked out when changing the frame.
    122.     if (rocketLauncher)
    123.     {
    124.         var rocketTexture : Texture2D = rocketGUI.texture;
    125.         rocketTexture.frame = rocketLauncher.ammoCount;
    126.     }
    127. }
    Obviously I must be doing two things wrong, one for each script I guess,
    Thanks
    AC
     

    Attached Files:

  7. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    hey Warped Axiom, did u get it working? If so I'd love to know what the trick is...

    BTW the bottom lager piece of code is just the fpsplayer script with the levelloadandfade line changed.

    AC
     
  8. WarpedAxiom

    WarpedAxiom

    Joined:
    Oct 27, 2006
    Posts:
    245
    Nope. It still claims it's an "unknown identifier" in the console.

    Edit: I opened the FPS tutorial and noticed the script was placed in the "misc scripts" folder, so I tried placing the script in my "scripts"-folder and then everything was just peach cobbler. I don't know if this helps you, though.
     
  9. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Did you make sure that the script is named LevelLoadFade.js?

    What compile error exactly are you getting? (/Applications/Utility/Console.app lets you copy paste the complete error string)
     
  10. WarpedAxiom

    WarpedAxiom

    Joined:
    Oct 27, 2006
    Posts:
    245
    No, it works for me now. I was just telling Targos how I made it work,
     
  11. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    So am I to edit the LevelLoadFade Script?
    Or does the fps Player script reference it and it does not require editing?

    I could see where to mod that one perhaps but not really understand. I need to reference to a "game over" level and a "mission accomplished" level as results of occurances in the game...
    Thanks Joachim
    AC
     
  12. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You dont edit the script. You just use it from another script in the way i posted above.