O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Unity 3.x Scripting

Book Description

Write efficient, reusable scripts to build custom characters, game environments, and control enemy AI in your Unity game with this book and ebook.

  • Make your characters interact with buttons and program triggered action sequences
  • Create custom characters and code dynamic objects and players’ interaction with them
  • Synchronize movement of character and environmental objects
  • Add and control animations to new and existing characters
  • Written in simple and step-by-step format with real life examples, this book is the only one in the market to focus on Unity Scripting

In Detail

Unity is a powerful tool for anyone who aspires to build interesting and sellable games. One of the exciting aspects of Unity is how easily it can be extended to build custom characters and game environments. Scripting is the key to accomplish all this, but it comes with a steep learning curve.

This book will teach you everything you need to know about scripting in Unity. It will take you through creating, including and tweaking scripts for achieving various tasks. Some of these tasks include controlling characters, animations, controlling objects, handling game data and enemy AI.

The book will begin by building scripts for simple tasks and continue to develop them for increasing level of difficulty. The first few chapters include scripting to build a custom game environment and creating characters and controlling them. Arranging camera views, controlling and creating animations are covered in the next set of chapters.

The book then moves on to build objects, animate them and code character interaction with them and create inventories where the characters can pick ammo, health and other such objects. Towards the end, the book teaches how to add background sound and discusses basic enemy AI scripting.

Table of Contents

  1. Unity 3.x Scripting
    1. Table of Contents
    2. Unity 3.x Scripting
    3. Credits
    4. About the Authors
    5. About the Reviewer
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Diving into Scripting
      1. Downloading and installing assets for this book
      2. Getting started with the game
      3. Available Character Controllers
      4. Interactive objects
        1. Triggers
        2. Buttons
          1. Base button script
          2. Activating platform status
        3. Explosion box
          1. The Update function
          2. The BOOM function
        4. Downloading the Detonator package
          1. Pressing the button
      5. Dynamic objects
        1. Moving boxes
        2. Triggered object
        3. Moving platform
        4. Moving the character with the platform
      6. Summary
    9. 2. Custom Character Controller
      1. Creating a controllable character
      2. Custom Character Controller
        1. Setting up the project
        2. Creating movement
          1. Manipulating character vector
          2. Register input from the user
          3. The Rigidbody component
        3. Jumping
          1. User input verification
          2. Raycasting
            1. Improving efficiency in raycasting
          3. Additional jump functionality
        4. Running
      3. Cameras
        1. Camera scripting
          1. Creating camera script
          2. Creating an enumeration list
          3. Writing functions
            1. The Initialize function
            2. Changing camera function
            3. Changing the camera values function
          4. Writing camera switching controls
          5. Character movement and camera positioning
          6. Updating camera type changing
          7. Influencing camera with a mouse
          8. Clamping angles
          9. Camera's late update
          10. Rotating character with a camera
      4. Animation controls
        1. Playing simple animations
          1. Start function versus Awake function
          2. Animation component and playing speed
          3. Animation scripting
          4. Walk, run, and idle animations
      5. Summary
    10. 3. Action Game Essentials
      1. Programming weapons and pickables
        1. Creating the base
        2. Programming the weapon
          1. The Shooting function
          2. Shooting cooldown
          3. Alternative shooting function
      2. Advanced animation system
        1. Working of an animation
          1. Animation mixing
          2. Animation script overview
      3. Weapon pickup
      4. Adding ammo and health pickups
      5. Creating a treasure chest
      6. Applying projectile fixes
      7. Tethering and soft body
        1. Tethering
          1. Creating a tether
        2. Creating assets
          1. Tether manager
          2. Creation of tether
          3. The StickySegment script
          4. Tether scripts overview
      8. Summary
    11. 4. Drag-and-Drop Inventory
      1. GUI basics
        1. GUI.Box
        2. GUI.Button
        3. GUI.Label
        4. GUI.TextField
        5. GUI.TextArea
        6. GUI.Toggle
        7. GUI.Toolbar and GUI.SelectionGrid
        8. GUI.HorizontalSlider and GUI.VerticalSlider
        9. GUI.HorizontalScrollBar and GUI.VerticalScrollBar
        10. GUI.BeginGroup and GUI.EndGroup
        11. GUI.BeginScrollView, GUI.EndScrollView, and ScrollTo
        12. Other GUI classes
      2. Drag-and-drop inventory
        1. Basics
        2. Inventory slots and draggable objects
        3. Working with GUI windows
        4. Inventory slots
        5. Patching the inventory
      3. Character customization
        1. 3D character avatar
          1. Dealing with a camera
          2. Adjusting the camera
          3. Window dragging limits
        2. Customization
          1. Setting up items
          2. Adding items
          3. Modifying character
          4. Reloading and inventory
          5. Finishing adjustments
      4. Summary
    12. 5. Dynamic GUI
      1. Radial health display
        1. The Health script
        2. Health display script
        3. Revisiting the Health script
        4. Hooking up objects to Inspector
      2. Creating items
        1. The Change_Item script
          1. Setting up the code
          2. Changing items
          3. Addition and removal
          4. Displaying items
          5. Increment controls
        2. Creating the UseItem script
        3. Revisiting the Change_Item script
        4. The PlayerStats script
        5. The TextManager script
        6. The textMesh script
        7. Revisiting the UseItem script
        8. Revisiting the Health script
      3. Creating armor
        1. The Armor script
        2. Revisiting the HealthBar script
        3. Revisiting the Health script
        4. Revisiting the UseItem script
      4. Creating the weapons
        1. The Change_Weapon script
        2. The UseWeapon script
        3. Revisiting PlayerStats
        4. Revisiting the textMesh script
      5. Scripting and displaying the score system
        1. The Score script
          1. Reading from the text file
          2. Writing to the text file
        2. The timer script
        3. Revisiting the textMesh script
      6. Displaying the objectives
        1. Revisiting TextManager
        2. Revisiting textMesh
        3. Hooking up HUD
        4. Game manager
        5. Health
        6. Item_Pic
        7. ItemMultiplier, highScoreDisplay, ObjectiveDisplay, scoreDisplay, and weaponDisplay
        8. saveDisplay
        9. Weapon_Pic
      7. Creating the targeting system
        1. Creating the Bezier equation script
        2. ArcBehaviour
        3. The moveObject script
        4. Hooking it up in the editor
      8. Summary
    13. 6. Game Master Controller
      1. Game manager theory
      2. Creating game managers
        1. Level streaming
        2. Mission creation
        3. Managing levels
        4. Save/load system
        5. Loading with checkpoints
      3. GameLoader
      4. Dynamic camera
      5. Audio
      6. Audio manager
      7. Summary
    14. 7. Introduction to AI Pathfinding and Behaviors
      1. Simple waypoint pathfinding
        1. Setting up the hierarchy
        2. Writing the waypoint display script
        3. Setting up the path arrays
        4. Creating the aiSimplePath script
          1. Declaring variables
          2. Starting up functions
          3. Traversing the path
          4. Shutting down the robot
          5. Hooking up the aiSimplePath script on Inspector
      2. Enemy statistics, shooting, and behaviors
        1. The enemyStats script
          1. Setting up variables
          2. Setting up functions
          3. Retrieving functions
          4. Manipulation functions
          5. Hooking up the enemyStats script on Inspector
        2. The Shoot script
          1. Setting up the script
          2. Writing shooting functionality
          3. Hooking up the Shoot script on Inspector
        3. The aiSimpleBehaviour script
          1. Setting up the script
          2. Behavior functions
          3. Additional functions
          4. Hooking up the aiSimpleBehaviour script on Inspector
      3. Returning to the aiSimplePath script
        1. Pursue functionality
        2. Revisiting the EnemyPath function
      4. The bulletCollision, ammoCollision, and AmmoInfo scripts
        1. Creating the bulletCollision script
          1. Hooking up the bulletCollision script on bullet's Inspector
        2. Creating the ammoCollision script
          1. Hooking up the ammoCollision script on enemy's Inspector
        3. Creating the AmmoInfo script
          1. Hooking up the AmmoInfo script on ammo's Inspector
      5. Summary
    15. A. Object-oriented Programming in Unity
      1. Object-oriented programming – basics
        1. Encapsulation
        2. Classes
        3. Constructors
          1. Code
        4. Inheritance
          1. Preparations
          2. Code
        5. Polymorphism
          1. Code
        6. Nested classes
      2. Summary
    16. Index