Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Tutorial Project: Stealth under Unity 5.x

Discussion in 'Community Learning & Teaching' started by Vagabond820, Mar 10, 2015.

  1. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,678
    This one is easy, give me something harder. :D

    You don't understand the need for this code because you're doing it wrong in the next code. :)

    The above code is the robots field of view. So if the field of view is 110 degrees we check if the angle between the direction to the player (the direction variable) and the robot's forward vector (transform.forward) is less than half of 110, i.e. 55 degrees on either side of the direction the robot is currently facing.

    If you compare your original code that didn't work to the code in the tutorial you should see your error.

    You had this line:
    Code (JavaScript):
    1. if (Physics.Raycast (transform.position+transform.up, transform.normalized,hit, 10f)
    The code in the actual tutorial is like this:
    Code (JavaScript):
    1. if(Physics.Raycast(transform.position+transform.up, direction.normalized, hit, 10f))
    The obvious difference here is transform.normalized vs direction.normalized. I'm surprised transform.normalized didn't throw an exception or error. It's completely nonsensical. :) Transform isn't a vector, it's a class. The raycast needs to know in which direction to do the raycast and direction is the vector towards the player that we initialized earlier in the code.

    And your edited code "works" because now you're actually raycasting in a direction, but by specifying transform.forward instead of the direction variable for the raycast you're only raycasting directly ahead of the robot. So you're ignoring the field of view calculation we did earlier, i.e. why you didn't understand the need for that part of the code.

    Basically this code does the detection in steps. First we use OnTriggerStay() to detect if the player is actually in range for any of our other checks. This is probably done for performance reasons I reckon. Then when the player is actually inside the robot's "sphere of influence" we check if the player is inside the robot's field of view. And if the player is within the robot's field of view we do the final raycast to see if we can actually see the player. He could be behind a wall or something after all. If this final check passes we know we can see the player.

    PS: I'm guessing you would have gotten some kind of error when you used transform.normalized as a direction for the raycast if you used C# instead of Unityscript/javascript. In my opinion you should consider switching. All official Unity teaching is done with C# now and most forum users use C# as well. Not to mention the plethora of help and documentation you'll find on the internet in general about C#.

    Edit: Yeah, you get this immediately if you use C#:
     
    Last edited: May 17, 2015
    CelticKnight likes this.
  2. CelticKnight

    CelticKnight

    Joined:
    Jan 12, 2015
    Posts:
    378
    Rightio, I had some problems with that code but now I think I've got the code right but I am still getting the same problems. This is the code:

    Code (JavaScript):
    1.     if (Physics.Raycast (transform.position+transform.up, direction.normalized,hit, 10f))
    2.                 if(hit.collider.gameObject.tag == "Player"){
    3.                     print("player can be shot...............");
    4.                     }
    5.                 }//end physics if*/
    Now using this code I get no collision registering with the raycast. Now is this a problem with JavaScript or have i missed out some essential code to get the collision working?

    At the moment the code, recognises the player is in the isTrigger zone, can see the player with that 55 degree FOV, but, not the raycast hit.
     
  3. kubajs

    kubajs

    Joined:
    Apr 18, 2015
    Posts:
    55
    @CelticKnight, if your other code is correct and Player is really in the robocop's field of view, the direction between robocop and player is lower than robocop's sphere collider radius and there is no obstacle between robocop and player (you have direct visibility between them), your condition should be true.
    Is Player properly tagged by "Player" tag?
    Do you get inside the first condition? (print / Debug.Log should help you find the answer if you put it inside the first if statement).
    if you get inside the first condition, I bet you Player object isn't just properly taggeed.
    try print(hit.collider.gameObject.tag) directly in the Physics.Raycast condition.
    Attaching my IsPlayerInSight method (C#) with some comments:

    Code (CSharp):
    1. private bool IsPlayerInSight()
    2.     {
    3.        // get vector from player to enemy position
    4.         Vector3 directionToPlayer = m_Player.transform.position - transform.position;
    5.         //get angle between line from enemy to the center of enemy view and line from enemy to player
    6.         float angle = Vector3.Angle(transform.forward, directionToPlayer);
    7.         //if player is outside the enemy view, return false
    8.         if (angle > AngleOfSight * 0.5f)
    9.         {
    10.             return false;
    11.         }
    12.         RaycastHit hit;
    13.         if (Physics.Raycast(transform.position + transform.up, directionToPlayer.normalized, out hit, m_SphereCollider.radius))
    14.         {
    15.             //I used different approach but the result should be the same as
    16.            // if(hit.collider.gameObject.tag == "Player")
    17.             if (hit.collider.gameObject == m_Player)
    18.             {
    19.                 return true;
    20.             }
    21.         }
    22.         return false;
    23.     }
     
    Last edited: May 20, 2015
    CelticKnight likes this.
  4. kubajs

    kubajs

    Joined:
    Apr 18, 2015
    Posts:
    55
    What went wrong with CCTVs in Unity5? I haven't met any issues except some minor nice to have features only like fog etc. I can really live without. But no issue with any code, dynamics, alarms etc. Everything worked flawlessly. Yeah I remember I spent about 1 hour of debugging just to realize I needed to call NavMeshAgent.Resume() to force lazy rusty robotGuard move once it was previously stoped ;). Bo no more other real problems.
    I can provide my code/settings if needed.
     
  5. CelticKnight

    CelticKnight

    Joined:
    Jan 12, 2015
    Posts:
    378
    Sorry it has taken me so long to get back to you, over the last three days I have been quite unwell, unfortunately, it was worse than "man-flu" :D.

    Anyway, using your reply and code as a guide I still couldn't work out why the normalizing function wasn't playing nice with the rest of my code. So, in the end I ditched my code and used what you had - of course, making the necessary changes, I really didn't want a boolean function, which I thought was a neat idea, by the way :). And the normalizing thingy seemed to work as I was getting hits when the raycast didn't actually hit the "player", not many more but enough to make a difference, which I suppose is what they are trying to accomplish with this code.

    I did however find a small mistake with your code you had: if angle is greater than FOV * 0.5 where it should've been less than, but, then again you were using it in a different fashion, returning a bool so it might be working in your context. In my case I got a shock when the print("we can 'see' player"); in the console collapse view got up over 3,000 so fast :p.

    So, thankyou so much for taking the time to help. Your time, patience (& code :cool:) is very much appreciated.
     
  6. HappyMilk

    HappyMilk

    Joined:
    May 25, 2015
    Posts:
    3
    Hi, I'm starting to use Unity for my school class. I'm doing the Stealth tutorial and am in 303. While trying to complete the DoorAnimation script, the console tells me there's an error in these three lines:
    Code (csharp):
    1. audio.clip = accessDeniedClip;
    2. if(other.gameObject == player || (other.gameObject.tag == Tags.enemy && other is CapsuleCollider))
    3. audio.clip = doorSwishClip;
    The program updated "audio.clip" to "GetComponent<AudioSource>.clip", and then I deleted ".clip", but neither of those methods works and I can't figure out how to fix it. Would you help me?
     
  7. kubajs

    kubajs

    Joined:
    Apr 18, 2015
    Posts:
    55
    GetComponent<AudioSource>().clip;
    as GetComponent is generic method so you need to add parentheses.
     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,670
    Don't forget they it's best to cache the reference to the audio source and use the reference it you need that audio component more than once.
     
  9. HappyMilk

    HappyMilk

    Joined:
    May 25, 2015
    Posts:
    3
    OK, I could swear on the bones of Eddard Stark that I had already tried with the parentheses... Anyway, that fixed the GetComponent error, thanks! I still have an error with the third line:

    Code (CSharp):
    1.     void OnTriggerExit(Collider other)
    2.     {
    3.         if (other == player || (other == Tags.enemy && other is CapsuleCollider))
    4.         {
    5.             count = Mathf.Max (0, count-1);
    6.         }
    7.     }
    It tells me: "error CS0019: Operator `==' cannot be applied to operands of type `UnityEngine.Collider' and `string'".
     
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,670
    That error is simply telling you exactly what you need to know. "The operator == cannot be applied to operands of type collider and string." Other is a reference to a collider. Tags.enemy is a string. Do you want to compare a collider to a tag? Or a tag to a tag? Perhaps try other.tag == Tags.enemy?
     
  11. HappyMilk

    HappyMilk

    Joined:
    May 25, 2015
    Posts:
    3
    Yes! That worked, I still have to know a lot about coding. Let me see, it was because I wrote "(Collider other)" in the first line right? Anyway, thank you very much. I normally try to figure things on my own in the tutorials, but it seems that my brain ran out of ideas in this part.
     
  12. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,670
    Well, you must write Collider collider as parameters in the parameter list for this function. And this is dictated by function's signature. You then need to know how to use the data being passed in. As this is a Collider, you can look up and see what items are accessible members, and one of these is the collider's tag.
     
  13. mikhailb1990

    mikhailb1990

    Joined:
    May 26, 2015
    Posts:
    1
    Hi, I am currently following the Stealth tutorial, and am on the Player movement section. Everything is working great, but I just can't seem to understand how the player moves in the game even though we aren't updating the position directly in any way through the code...is mecanim doing this for us? Or am I just missing something. Again - my player moves just fine, I just can't understand how, so I'm posting this

    PS: by movement, I just mean the player's position. I understand the rotation stuff well. Thanks!

    Update: Nevermind, figured it out - it's due to the option of 'Apply root motion' in the Animator component. Please correct me if I'm wrong :)
     
    Last edited: May 28, 2015
  14. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,670
    I believe the order is:
    Player asks Mecanim to use an animation.
    The animation has "root motion", which means the motion of the object is based on the animation.
    The root motion moves the GameObject.
     
  15. pwnsdisease

    pwnsdisease

    Joined:
    May 27, 2015
    Posts:
    5
    What effects package?
     
  16. pwnsdisease

    pwnsdisease

    Joined:
    May 27, 2015
    Posts:
    5
    The second video in the tutorial at about 3:45 we turn the camera_main background down from blue to black. In the video when he does this his skybox turns black to match. Mine does not and I have no idea why.
     
  17. e_Glyde

    e_Glyde

    Joined:
    Apr 20, 2013
    Posts:
    42
    You're using Unity 5 so.
    (Next to File and Edit)Assets->Import Package->Effects

    Edit(pictures :D):
     
  18. e_Glyde

    e_Glyde

    Joined:
    Apr 20, 2013
    Posts:
    42
    For the sky you can change the camera's background to black like this(only in camera's viewpoint):
    1:Go into the lighting tab and remove the skybox(click the box and press delete).

    2:Go to the camera and change the "Background" color from blue to black.


    How to change the sky to black(for lighting, the scene and camera):
    1:Make a material, change the shader to skybox/procedural and set the exposure to "0".
    2:Go into the lighting tab and set the skybox shader to the newly created material
     
  19. e_Glyde

    e_Glyde

    Joined:
    Apr 20, 2013
    Posts:
    42
    For the global fog(steps in picture form):



    Done.And to edit how the fog looks(distance fog and color and stuff):

    Use the old way(above tab.btw you don't need to have the box checked,only for editing.).
     
  20. e_Glyde

    e_Glyde

    Joined:
    Apr 20, 2013
    Posts:
    42
    This is assuming you are using the Deferred rendering.
     
  21. Barianos

    Barianos

    Joined:
    Apr 1, 2015
    Posts:
    4
    Just my 2 cents for anyone looking to start the projects.
    After quickly going through this topic i though it was going to be a really hard task to try and do this game in Unity 5. But I liked the challenge and went for it. Other than the lightmapping that Vagabond820 gave us some info on, the only thing that got me frustrated was some animations. There were many more little details, but they all were so obvious it was no big deal.
    If you have gone through the previous tutorials, you should not really have a problem, you have learned enough to go make the tweaks yourself, and it sure is a good way to get to know Unity better.
    So, if you have done the other tutorials on the page, don't be frightened by this one. And if you haven't, don't jump into this one just because you like it more, or because you believe you are already quite good at game design or game developing. Theese tutorials are more about learning to use unity that to learn basics of developing, so start from the start....
     
    CelticKnight likes this.
  22. Texian

    Texian

    Joined:
    Mar 13, 2013
    Posts:
    7
    *grumble*
    Ok this one's starting to drive me crazy. I've been able to adjust to Unity 5 pretty well and gotten some good learning experiences adapting this lesson to the new engine, but for some reason the door thing has me stymied. For whatever reason, the silly things just won't open when I get close to them. Their sphere colliders are plenty big and are marked as triggers, I've got no errors in my script, the player is tagged "Player"....I'm stumped as to what I'm doing wrong.
     

    Attached Files:

  23. e_Glyde

    e_Glyde

    Joined:
    Apr 20, 2013
    Posts:
    42
    Can I see a screenshot of the animator window?
    Also does the animations even work?
     
  24. Texian

    Texian

    Joined:
    Mar 13, 2013
    Posts:
    7
    Here ya go. And the animations do seem to work in the Preview pane.
     

    Attached Files:

  25. e_Glyde

    e_Glyde

    Joined:
    Apr 20, 2013
    Posts:
    42
    Does the "isOpen" bool actually turn on and off?
     

    Attached Files:

  26. Kibo_Ilen

    Kibo_Ilen

    Joined:
    Jun 5, 2015
    Posts:
    3
    Character not moving at all, the turns are okay but no walking or running any help please
     
  27. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    This may not be possible, but please don't ever remove this kind of stuff. I have Unity 5 and would like to one day give this a go (when I'm a bit more experienced with Unity). This tutorial is exactly what I was looking for too, minus the issues with making it work in Unity 5. Even with that, I'm sure there are tonnes of concepts in this that can be used from this project in our various projects. I would love it if one day this was 100% Unity 5 compatible (by then maybe 6 or higher will be out though lol), but I'm happy just to even have this project and tutorial around to get a more higher level of how some stuff works.
     
  28. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,678
    For anyone still doing this tutorial I (finally) completed my version of the project. You can find it here:
    http://twiik.net/unity/stealth-extended

    It's very different from the tutorial, but it still has the same premise - get to the lift. :p
     
    Hormic likes this.
  29. jackhearts

    jackhearts

    Joined:
    Apr 5, 2013
    Posts:
    103
    Thought I'd have a go at this and work around any unity 5 issues, so far there hasn't really been many. As for fog, right underneath Deferred in the rendering path field is Legacy Deferred (light prepass). That to me sounds like what would have been used in the vid and seems to create a similar effect. The settings are still a bit messed around but it's the effect that matters. Saves messing around manually adding fog effects if you just want to crack on.

    btw, I may have missed it in the vid but what's the fog actually there for?
     
  30. Campusanis

    Campusanis

    Joined:
    Jul 9, 2013
    Posts:
    5
    I just finished chapter one today and am getting along nicely on Unity 5.1.1f1.

    Now I've got a major problem, though: Unity crashes whenever I try to bake the lightmap on "High Resolution Parameters". Low Resolution was no problem, but they want you to do a high quality bake at the end of chapter one, and I've had three unsuccessful attempts. Anyone else getting this? Trouble shooting this is very time consuming because every new bake takes a few hours...

    (I'm on a 3Ghz quad core with 8GB memory, so a reasonable machine I'd say).
     
  31. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,678
    I just went with all realtime lighting. Baking things, even in real life :p, is something I thoroughly dislike.

    Remember that the original tutorial was for Unity 4.x which used Beast and now in Unity 5.x we use Enlighten. The baking process is probably different between the 2 lightmapping engines, and "High resolution" in Enlighten could for instance be overkill while in Beast it was acceptable.

    Also, waiting hours for a bake seems ludicrous to me, especially for a tutorial. :) My advice would be to just go with whatever works and focus on finishing the tutorial. The scene is not very optimized which may cause troubles for the lightmap baking process as well.
     
  32. Campusanis

    Campusanis

    Joined:
    Jul 9, 2013
    Posts:
    5
    Thanks for the info! I actually ended up upgrading to Unity 5.1.3 (might as well do it before I start any real projects) which solved the issue AND made things go faster. :)
     
  33. jackhearts

    jackhearts

    Joined:
    Apr 5, 2013
    Posts:
    103
    I see Twiik's ludicrous and raise you extraordinary for the length of time to bake. I'm on a 2.3ghz quad 4th gen i7 laptop and it only took about 20 minutes to bake.
    For a tutorial I wouldn't normally bother with high quality bakes anyway, it's probably the least significant thing to be concerned with.
     
  34. Campusanis

    Campusanis

    Joined:
    Jul 9, 2013
    Posts:
    5
    Yeah, now it takes about that time for me as well, thankfully!
    I was looking at it more like a "test" of the engine for me (in addition to the tutorial aspect), just wanted to see if it's at all possible to do high quality bakes on my machine.
     
  35. Campusanis

    Campusanis

    Joined:
    Jul 9, 2013
    Posts:
    5
    I've got another minor nit-picky question. ;) I noticed that this is happening in the tutorial videos, too, but I'm just wondering if there's a way to smooth this out:

    When my player goes from running to being idle (i.e. when I release a movement key after holding it down), sometimes the player will noticeably "jump" backwards a little, as if he were pushed back by the animation. I've tried fiddling with the settings of the input axes, particularly sensitivity, but no help...
     
    Last edited: Sep 8, 2015
  36. Morello05

    Morello05

    Joined:
    Sep 19, 2015
    Posts:
    16
    This project uses some elements that I need but I feel it's pointless to continue if I'm just getting stuck 10 minutes into the first video. Why not add text to the video explaining how to get round all the changes done in Unity 5. Why not just redo it in a totally new project but use all the same aspects of this one. As a new user to Unity this is very frustrating. I understand Unity is now free to download excluding Pro but what's the point in making it free when most of your tutorials are out of date. Code is being changed and I'm finding it hard keeping up with it, it's easy to follow a video and just copy and in turn learn from that. The problem is when things are moved around not updated and then you expect people to constantly google for fixes. I'm getting sick and tired of having to find what I need only for someone to explain in another confusing way and then I never end up finding it. My advise just remove this video and redo something similar for Unity 5.
     
  37. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,678
    If it's too hard just start with a tutorial made with and for Unity 5 and move back to this when you're more skilled. Or just ignore this one completely. Why do you have to drag everyone down with you? There's been a number of old Unity tutorials that's been removed, probably because of people like you. Tutorials which even though they were made for Unity 2 or 3 contained many cool assets and neat scripts useable today. Now they are gone forever, unless you actually managed to save them before they were deleted.

    In my head it's so friggin' simple. Just have a Unity 2.x, 3.x, 4.x, 5.x sub section in the tutorial section, but nooo, both people like you and Unity themselves make this sound like it's impossible, and probably solely because a lot of people have to go into the wrong section and whine about how the tutorial doesn't work in Unity X anymore when there's disclaimers everywhere!

    I just completed this tutorial in Unity 5 and there was perhaps 2 or 3 spots where I had to do something slightly differently than how the tutorial explained it for Unity 4. Not exactly a big deal.

    And telling them to redo something similar in Unity 5 is ridiculous. If it was that easy they would. How long do you think it took to make this tutorial? And there may even be some people who still prefer that it's a Unity 4 tutorial and not a Unity 5 tutorial.

    PS: There's a ton of unofficial Unity 5 tutorials you can do as well. Just google and see.
     
  38. cams01

    cams01

    Joined:
    Aug 17, 2015
    Posts:
    5
    Finished the tutorial but have a problem. When the enemy tries shooting me, at the AI script nav.stop() is called. So he stops. But even after I escape, the enemy just stays in place. If other enemies are wandering, they trigger the last sighting reset and thus stops the alarms etc. But if all 3 tries to shoot me, they are all stopped and the resets are never triggered.
    The point is that after nav.stop() is called as they shoot, something like nav.start() is never called again. Is there a bug in the tutorial? (seems unlikely, someone should have mentioned it all these years) Or did I make a mistake?

    edit: It's nav.resume(). Shouldn't that be mentioned somewhere?
    edit: Yep, adding them at the start of Chasing and Patrolling fixed the problem. But still, did I make a mistake elsewhere? Noone seemed to have mentioned this.
     
    Last edited: Dec 5, 2015
  39. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,678
  40. cams01

    cams01

    Joined:
    Aug 17, 2015
    Posts:
    5
    Well, not sure how I missed that. How stupid of me. :p
    In my defense though, I mostly focus on the Youtube comments. as thoroughly reading every post in this thread after every video for tips and possible bugs is not practical. And I was also focusing on debugging and trying to solve the problem on my own, which I succeeded in doing. So that's nice.
     
  41. BCU_Euden

    BCU_Euden

    Joined:
    Mar 14, 2013
    Posts:
    11
    I'm going through this tutorial as I was meant to do when it first came out, using the current version of Unity, could someone please explain the light baking steps for me using the new system? I'm unable to continue until I do this.
     
  42. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,678
    Why exactly? Baking the lights has nothing to do with the actual tutorial, it's just part of setting the scene. Either just skip the lights, find some other tutorial on Enlighten or just try it yourself and see what you get.

    Personally I advise you to learn Enlighten in a much smaller scene so you don't spend hours baking.
     
  43. blizzardeagle

    blizzardeagle

    Joined:
    Sep 27, 2015
    Posts:
    3
    Another issue I came across, are the GUITextures, which I don't have under Unity 5.3. Instead I use UI->Image, and fit it over the complete scene (manually). I set the alpha channel to 0, so that we can at least see our scene when designing the fader. Then, I simply replaced all
    "guiTexture" by "gameObject.GetComponent<Image>()". Further, I've edited the Awake function as follows:
    Code (CSharp):
    1.     void Awake(){
    2.         (gameObject.GetComponent<Image>()).color = Color.black;
    3.     }
    This means that I don't create a rectangle and that I set the color to black to start with.
     
  44. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,670
    Using a UI Image should be fine (I can't remember the use case here), but the GUITexture still exists as a component. It's been deprecated as a premade GameObject, but you can still make GUITextures by creating an empty GameObject and adding the GUITexture component.
     
  45. blizzardeagle

    blizzardeagle

    Joined:
    Sep 27, 2015
    Posts:
    3
    I'd like to get back to groo79's question:

    Does anyone else needed to apply the same fix? Why do we need to do this in order to make the enemy stop running in circles?

    Thanks!
     
  46. ladyonthemoon

    ladyonthemoon

    Joined:
    Jun 29, 2015
    Posts:
    236
    Last edited: Jun 26, 2016
  47. ladyonthemoon

    ladyonthemoon

    Joined:
    Jun 29, 2015
    Posts:
    236
    All right, I'm going to post my personal experience with this tutorial, maybe it will help some people. :)

    Project pages:

    I accidentally found out the project pages; they can be found here: http://unity3d.com/learn/tutorials/...-only/stealth-project-overview?playlist=17168

    The link in the very first post of this thread is broken: http://unity3d.com/learn/tutorials/projects/stealth-tutorial-4x-only

    Video 3 "Alarm Lights"

    The "AlarmLight" script needs a slight update so that it works. Fortunately, Unity has a very handy tool that upgrades the scripts for us. When you write the script as it is in the video:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AlarmLight : MonoBehaviour
    5. {
    6.     public float fadeSpeed = 2f;            // How fast the light fades between intensities.
    7.     public float highIntensity = 2f;        // The maximum intensity of the light whilst the alarm is on.
    8.     public float lowIntensity = 0.5f;       // The minimum intensity of the light whilst the alarm is on.
    9.     public float changeMargin = 0.2f;       // The margin within which the target intensity is changed.
    10.     public bool alarmOn;                    // Whether or not the alarm is on.
    11.  
    12.  
    13.     private float targetIntensity;          // The intensity that the light is aiming for currently.
    14.  
    15.  
    16.     void Awake ()
    17.     {
    18.         // When the level starts we want the light to be "off".
    19.         light.intensity = 0f;
    20.    
    21.         // When the alarm starts for the first time, the light should aim to have the maximum intensity.
    22.         targetIntensity = highIntensity;
    23.     }
    24.  
    25.  
    26.     void Update ()
    27.     {
    28.         // If the light is on...
    29.         if(alarmOn)
    30.         {
    31.             // ... Lerp the light's intensity towards the current target.
    32.             light.intensity = Mathf.Lerp(light.intensity, targetIntensity, fadeSpeed * Time.deltaTime);
    33.        
    34.             // Check whether the target intensity needs changing and change it if so.
    35.             CheckTargetIntensity();
    36.         }
    37.         else
    38.             // Otherwise fade the light's intensity to zero.
    39.             light.intensity = Mathf.Lerp(light.intensity, 0f, fadeSpeed * Time.deltaTime);
    40.     }
    41.  
    42.  
    43.     void CheckTargetIntensity ()
    44.     {
    45.         // If the difference between the target and current intensities is less than the change margin...
    46.         if(Mathf.Abs(targetIntensity - light.intensity) < changeMargin)
    47.         {
    48.             // ... if the target intensity is high...
    49.             if(targetIntensity == highIntensity)
    50.                 // ... then set the target to low.
    51.                 targetIntensity = lowIntensity;
    52.             else
    53.                 // Otherwise set the targer to high.
    54.                 targetIntensity = highIntensity;
    55.         }
    56.     }
    57. }

    and then go back to the editor, a window pops up reading:

    ObsoleteAPIMessage.JPG
    Note: the "API Update required" window will not open if you have errors in the console. You'll have to fix these errors before it does and you can update the script.

    If you click on "I Made a Backup. Go ahead!", the engine will rewrite the script accordingly to the current syntax:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AlarmLight : MonoBehaviour
    5. {
    6.     public float fadeSpeed = 2f;
    7.     public float highIntensity = 2f;
    8.     public float lowIntensity = 0.5f;
    9.     public float changeMargin = 0.2f;
    10.     public bool alarmOn;
    11.  
    12.     private float targetIntensity;
    13.  
    14.     void Awake ()
    15.     {
    16.         GetComponent<Light>().intensity = 0f;
    17.         targetIntensity = highIntensity;
    18.     }
    19.  
    20.     void Update ()
    21.     {
    22.         if (alarmOn)
    23.         {
    24.             GetComponent<Light>().intensity = Mathf.Lerp (GetComponent<Light>().intensity, targetIntensity, fadeSpeed * Time.deltaTime);
    25.             CheckTargetIntensity ();
    26.         }
    27.         else
    28.         {
    29.             GetComponent<Light>().intensity = Mathf.Lerp (GetComponent<Light>().intensity, 0f, fadeSpeed * Time.deltaTime);
    30.         }
    31.     }
    32.  
    33.     void CheckTargetIntensity ()
    34.     {
    35.         if (Mathf.Abs (targetIntensity - GetComponent<Light>().intensity) < changeMargin)
    36.         {
    37.             if (targetIntensity == highIntensity)
    38.             {
    39.                 targetIntensity = lowIntensity;
    40.             }
    41.             else
    42.             {
    43.                 targetIntensity = highIntensity;
    44.             }
    45.         }
    46.     }
    47. }

    Very helpful; thank you guys! :)

    To test the script (and the sound effects) in the game, just check the alarmOn variable in the script, make the audio sound of the megaphones "play on awake" and click on "play".

    Test.jpg
     
    Last edited: Jun 29, 2016
    hopeful likes this.
  48. ladyonthemoon

    ladyonthemoon

    Joined:
    Jun 29, 2015
    Posts:
    236
    Video 5 - Screen Fader

    The way described in the video being apparently completely deprecated, I had to find another way. Fortunately for me, I just finished the Survival Shooter tutorial and, in this tutorial we make a screen fader.

    For now, I just made the first screen fader, the fade out part that will play when we start the game or load the scene. To make this screen fader:
    • right click in the hierarchy view, go down to "UI" and create an "image",
    ScreenFader1.jpg
    • rename "image" into "ScreenFader" and double-click on it,
    • to see it better, click on the "2D" icon in the scene view,
    ScreenFader2.jpg
    • reset the "RectTransform" component to 0, 0, 0,
    ScreenFader3.jpg
    • click on the rect transform icon on the top left and then alt-click on the down right icon so that the image stretches to the entire screen,
    ScreenFader4.jpg
    • in the "Image" component, click on "Color" and select black in the colour picker; make sure the "A" (alpha layer) is set to 255,
    ScreenFader5.jpg

    See continuation below.
     
    Last edited: Jun 27, 2016
    hopeful likes this.
  49. ladyonthemoon

    ladyonthemoon

    Joined:
    Jun 29, 2015
    Posts:
    236
    Video 5 (continuation)

    Now that the black screen is made, we need to animate it so that it fades out:
    • with "ScreenFader" selected, go to Window and click on "Animation",
    ScreenFader6.jpg
    • in the "Animation" view, click on "create" to create an animation clip for the screen fader and name it "ScreenFaderClip",
    • click on "Add property", expand "Image (script)", select "Color" and click on the + to create the property (from now on, the animation window will be in "record" mode),
    ScreenFader7.jpg
    • expand the property so that the colours and the alpha layer are visible,
    • on the right, grab the second column of keyframes by grabbing the first on the top and drag them up to 2:30,
    ScreenFader8.jpg
    • once there, go to the colours and change "Color a" (the alpha layer) from 1 to 0 (Careful! At 0:00, "color a" must be set to 1; at 2:30 "color a" must be set to 0),
    • stop the recording by clicking on the red circle on the top left of the property,
    ScreenFader9.jpg
    • in the project view, select "ScreenFaderClip" and uncheck "loop time" so that the clip doesn't loop.
    ScreenFader10.jpg

    Now with "ScreenFader" selected, go to Window and click on "Animator". This view shows the state machine that was created with the animation. This state machine will allow us to create the fade in animation.

    To be continued.
     
    Last edited: Jun 30, 2016
    hopeful likes this.
  50. ladyonthemoon

    ladyonthemoon

    Joined:
    Jun 29, 2015
    Posts:
    236
unityunity