Download presentation
Presentation is loading. Please wait.
1
Week 6: Time and triggers!
2
Contents 13 Slides Update/ Fixed update Time / Delta Time
Basic Controller Collision Triggers Tags RigidBodies Basic Movement Old UI / New UI Boolean Toggle
3
Update/ Fixed update SO far we’ve been using “Update” which calculated everything frame by frame. Every frame, update checks the state of everything, such as button pushes, collision, scores and variables, and responds with the correct methods, animations, and placement of every Game Object within the scene. But sometimes, we have more complicated scenes, with a LOT more calculations, such as giant particle explosions, weather effects, or hundreds of moving assets on screen. Since these frames with all the extra action take longer to calculate, they also take longer to update, causing the game to have some strange, slow-mo parts. We can smooth this out by using the built – in FixedUpdate(), which runs on on a predefined number of frames per second, instead of “all the frames we can calculate per second.” This is especially used when we have RIGIDBODIES in our scene.
4
Time / Delta Time Since game engines think in frame rates, if we want it to use a real time, we have to specify that’s what we want. We do this by using Time.deltaTime If we want to make a timer, we have to use a FLOAT! EX: public float timeLeft = 25f; timeLeft -= Time.deltaTime;
5
Basic Character Controller
Now that you can use Time.deltaTime, you can move things over the span of several frames! We will make a very basic controller, that looks for keyboard input and either tranfroms.Translate or transform.Rotate over time. EX: public GameObject MyCar; MyCar.transform.Translate (Vector3.forward * PlayerSpeed * 10 * Time.deltaTime);
6
Collision In order for objects to appear solid in a game engine, we need COLLISION! In most engines, this is made by generating simple shapes such as a cube, around the model, and checking (every frame) if anything has intersected the colliders bounds and does something based on that information. In Unity, you can use a variety of COLLIDERS, from the humble, simple box (faster calculations) to an exact replica of your model ( more visually accurate collision, but much more calculation heavy) and several primitives (simple shapes) in between. Once the collision has been detected, we can run code, such as “remove HP” or “end game!” Select object --> Component --> physics --> pick collider shape
7
Triggers TRIGGERS: if you would like your collision box to instead work as a trigger, You can tick the IsTrigger Boolean checkbox in the Collider Component section of the inspector: You can use code to define an event to happen when the object enters the triggers space, (Occurs once) When it is staying in it, (Occurs continuously) or when it exits (occurs once).
8
Tags TAGS let you add a BULK NAME to any gameobjects, allowing you address many different things as a group. Examples Might be “ENEMY” or “DESTROYABLE”. A Tag is a STRING owned by that object, and is addressed as such. From there, you can also check the name or tag of the triggering object, to define what reaction you would like. if (other.gameObject.name == "LapGate") { Do these Instructions ;} Tags can be things like enemies, power ups, cabbages, players, push-able cubes, etc.
9
RigidBodies If you would like the object to react to built in physics calculations, you also need to add a Rigidbody . RigidBodies have several Important options: Use Gravity: Controls whether gravity affects this Rigidbody. Is Kinematic: Controls whether physics affects the Rigidbody. They are added in the same fashion as collision: Select object --> Component --> Physics --> RigidBody
10
Destroy() MWAHAHAHAHAHAHA
Destroy is a special built-in function, that removes an object from both the SCENE and from MEMORY. This is especially helpful in situations where we instantiate a lot of models, such as bullets. Required arguments are “ What should we destroy?” Example: Destroy ( other.gameObject);
11
NEW UI As of Unity 4.6, Unity has implemented a new system for creating GUI’s. Now, Unity uses a Flash-like, WYSIWYG interface. It begins with a CANVAS object that acts like a 2D piece of cellophane taped to your camera, or placed in your level. Then, UI objects are placed on that cellophane. An EVENT SYSTEM can call different methods based on interaction with those objects, such as a button click. UI Object types include Buttons, Sliders, Labels, Panels, Scrollbars, and Interactive Input Fields the player can type in. To use Unity UI, add the library at the top of your script: using UnityEngine.UI;
12
Old UI To add to our interactivity, let’s make a start button! The start button is part of the built in OnGUI method, so we need to include it there, BUT it also includes an IF statement! The button, by it’s very nature, already knows that it uses a click event, so we do not need to give it that information. Also, since the button is part of an if statement (A question!), it is no longer an instruction, and does not get a semicolon! void OnGUI () { if ((GUI.Button (new Rect (700, 2, 100, 100), “Text on the button"))) // Do These Instructions; }
13
Boolean Toggle You can use programming shorthand to create a Boolean toggle, changing true to false, and false to true: Bool = !Bool ; This is short hand for: If (bool == true) { bool = false; } Else bool = true;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.