260 likes | 376 Views
SE 320 – Introduction to Game Development. Lecture 8: Animations, GUIs, Debugging and IDEs Lecturer: Gazihan Alankuş. Please look at the last two slides for assignments (marked with TODO ). Topics Today. Animations Sources Scripting GUIs GUIText and GUITexture OnGUI magic
E N D
SE 320 – Introduction to Game Development Lecture 8: Animations, GUIs, Debugging and IDEs Lecturer: GazihanAlankuş Please look at the last two slides for assignments (marked with TODO)
Topics Today • Animations • Sources • Scripting • GUIs • GUIText and GUITexture • OnGUI magic • Debugging • Really debugging with breakpoints in MonoDevelop • Making the game pause before the next Update with Debug.Break() • Visual Studio and ReSharper
Animations • Why do we need them? • Setting position/rotation in the Update() function can be limiting • Sometimes you want to visually design how things will move • What are they? • Some motion information in a component called Animation • Where do they come from? • Create visually in Unity • Unity Window->Animation • Create in modeling/animation tools • Blender, 3DS Max, Maya, etc. • How can we use them? • In code, you tell Unity which animation to start • The “activate trigger” script is an example
Update() vs. Animations • In Update(), you can animate objects by setting their properties in code (location, rotation, scale, etc.) • Pros • Your calculations can be advanced (procedural animations, etc.) • Motions can depend on user input or other events in the game • Cons • For simple motions it can be too much work • E.g., a platform that does the same motion over and over • It can be difficult to adjust the motion so that it is • visually pleasing • exactly how you want it to be
Update() vs. Animations • With animations, you can visually design how things will move • Pros • It’s easier to make things move how exactly you want them to • It’s easy to adjust speeds throughout the motion, implement curved motions and make motions visually pleasing • Cons • Static motions, cannot react to user input or other events in the game (blending, switching, mixing can help)
Animations in Unity • You record the motion • You start playing that motion in game
Animations in Unity • You record the motion • You start playing that motion in game
Recording Animations in Unity • Window -> Animation opens the Animation pane • Add an animation component to the game object you want to animate (or let Unity do it for you while recording)
The Animation Pane record, play, etc. curve for z (position in time) timeline (can drag with mouse) other objects in your scene keyframes (pinned values in time)
Keyframe Animation • “I want the bus to be at this bus stop at 7:40, then I want it to be at this other bus stop at 7:45” • The bus driver does whatever is necessary to obey this. • You say “I want my cube to be here at time 0, I want it to be there at time 5, I want it to be there at time 8” • Unity calculates the motion that is necessary to make it happen
How to Create a Keyframe Animation • Select the object you want to animate • Open the animation pane and press record (top-left corner) • Make your object how you want it at the beginning of the animation • Drag to a future time • Move/rotate/scale the object to how you want it at this time • Repeat 4-5 until you are done. • Any time, you can drag time or press play to review the current state of the animation
Some Details • Pressing the record button • automatically adds an animation component if the object does not have one already • adds a new animation file if there was not one already in the animation component • You can have many animations for an object • In code you can play any of them, blend between them, etc. • Practice with the animation pane to find alternative ways of doing things (adding keyframes manually, dragging keyframes, etc.)
Animations in Unity • You record the motion • You start playing that motion in game
How to Play Animations • Automatically (easiest) • When “Play Automatically” ischecked, Unity will play the animation in the “Animation” property. • From code • animation.Play(“animationName"); • animation.CrossFade(“animationName2"); • Read the entry for the Animation class in the scripting reference • Can change speed, stop, mix, etc.
Animations vs. Update() • Animations are static but easy, you tell them to play and they play for many frames • Changing position in every Update() call is messy • Is there anything in between? • Yes! Tweening. • GoKit: https://github.com/prime31/GoKithttp://forum.unity3d.com/threads/133823-Prime31-GoKit-Tween-Library-Live
GoKit • Tell Unity to move an object to a certain point in four seconds, without having to do it in the Update() function • transform.positionTo(4f, new Vector3(10, 10, 10 )); • Many options to make it prettier. Look at the wiki • https://github.com/prime31/GoKit/wiki
Topics Today • Animations • Sources • Scripting • GUIs • GUIText and GUITexture • OnGUI magic • Debugging • Really debugging with breakpoints in MonoDevelop • Making the game pause before the next Update with Debug.Break() • Visual Studio and ReSharper
GUIs • Display scores, lives, etc. Any kind of textual or image-based output on the screen. • GUIText and GUITexture • Add them through Game Object -> Create Other. • Can be difficult to make them work otherwise. • Call their functions to do whatever you want to do with them • guiText.text = newScore + “ points”; //for example
More Complex GUIs • OnGUI() function of MonoBehavior • This is not the kind of functions you are used to! • Unity does unusual things with it. You have to read the documentation carefully. • Calls it every frame but creates only in the beginning void OnGUI () { if (GUI.Button(new Rect (10,10,150,100), "I am a button")) { print ("You clicked the button!"); } }
GUIs • You can use buttons for input (menus, etc.) • You can use text and images for output.
Topics Today • Animations • Sources • Scripting • GUIs • GUIText and GUITexture • OnGUI magic • Debugging • Really debugging with breakpoints in MonoDevelop • Making the game pause before the next Update with Debug.Break() • Visual Studio and ReSharper
Debugging • Debug.LogError • Debug.LogWarning • These let you display messages that won’t get lost in many others • Debug.DrawRay() • Debug.DrawLine() • These let you draw things in the scene view to see what exactly is going on in the game • Debug.Break() • This lets you make Unity pause itself after this frame
Debugging with MonoDevelopwhile Coding in Visual Studio at the Same Time • Do this once after you created your Unity project • Edit->Preferences->External Tools is where you set the IDE that Unity will fire • Select Visual Studio • Double-click a script, this will create the Visual Studio project • Select MonoDevelop • Double-click a script, this will get the project in MonoDevelop’srecents • Select Visual Studio again • Close everyting • Do this at every run • Run MonoDevelop without running Unity • Select the project that is already in the recent projects • Run->Debug • This will run Unity • Set a breakpoint in MonoDevelop and see how it stops at that breakpoint • Run Visual Studio by either • running it from the start menu, • Or ensuring Edit->Preferences->External Tools is Visual Studio and double clicking on a script. • This does not affect MonoDevelop
The BEST Development Environment for Unity Scripting that I Know of • Visual Studio + Resharper for coding • I asked for a classroom license. I also sent an e-mail. Still waiting. In the meantime go ahead and install the 30 day version. • MonoDevelop for occasional line-by-line debugging (explained towards end of presentation) • If you are using any other way, you would certainly develop a better game if you listen to me • (If you disagree, talk to me before discarding my advice… I have worked on large projects.)
No Homework! • Good luck at the exam!