






Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Prepare-se para as provas
Estude fácil! Tem muito documento disponível na Docsity
Prepare-se para as provas com trabalhos de outros alunos como você, aqui na Docsity
Encontra documentos específicos para os exames da tua universidade
Prepare-se com as videoaulas e exercícios resolvidos criados a partir da grade da sua Universidade
Responda perguntas de provas passadas e avalie sua preparação.
Ganhe pontos para baixar
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Introdução a scripts com Unity 3D. Documento em inglês.
Tipologia: Notas de estudo
1 / 10
Esta página não é visível na pré-visualização
Não perca as partes importantes!







Contents
1. Aims of this tutorial Scripting is how the user defines a game’s behaviour (or rules) in Unity. The recommended programming language for Unity is Javascript , however C# or Boo can also be used. This tutorial will cover the fundamentals of scripting in Unity and also introduce key elements of the Application Programming Interface (API). You can think of the API as code that has already been written for you which lets you concentrate on your game design and also speeds up development time. A good understanding of these basic principles is essential in order to harness the full power of Unity. 2. Prerequisites This tutorial focuses on the scripting element of Unity, it is assumed you are already familiar with Unity’s interface (if not you should read the Unity GUI tutorial). In order to make scripting easier to understand, it is preferable to have a code editor that has syntax highlighting support for Javascript. This means that reserved words (syntax used by Javascript itself) are coloured differently than user defined words. One such editor is SubEthaEdit. NB : any text that requires the user to take an action begins with a ‘-’.
3. Naming Conventions Before we begin, it is worth mentioning some conventions in Unity. Variables - begin with a lowercase letter. Variables are used to store information about any aspects of a game’s state. Functions - begin with an uppercase letter. Functions are blocks of code which are written once and can then be reused as often as needed. Classes - begin with an uppercase letter. These can be thought of as collections of functions. Tip: When reading example code or the Unity API, pay close attention to the first letter of words. This will help you better understand the relationship between objects. 4. Player Input For our first program we’re going to allow the user to move around in a simple game world.
We’re now ready to start game programming. We’re going to allow the player to move around the game world by controlling the position of the main camera. To do this we’re going to write a script which will read input from the keyboard, then we attach (associate) the script with the main camera (more on that in the next section).
var speed = 5.0; function Update () { var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed; var z = Input.GetAxis("Vertical") * Time.deltaTime * speed; transform.Translate(x, 0, z); }
Earlier in the tutorial we mentioned that it would be possible to assign the variables via code (as opposed to the Unity GUI), let’s take a look at how you would do that.
7. Instantiate It is often desirable to create objects during run-time (as the game is being played). To do this, we use the Instantiate function. Let’s show how this works by instantiating (creating) a new game object every time the user presses the fire button (either the left mouse button or left ctrl on the keyboard by default). So what do we want to do? We want the user to move around as usual, and when they hit the fire button, instantiate a new object. A few things to think about:
8. Debugging Debugging is the skill of finding and fixing human errors in your code (ok let’s call them mistakes!). Unity provides help via the Debug class, we’ll now look at the Debug.Log() function.
The Log() function allows the user to send a message to the Unity Console. Reasons for doing this might include:
Another useful feature for debugging is exposing a private variable. This makes the variable visible in the Inspector View when the Debug mode is selected, but it cannot be edited. To demonstrate this, we’ll expose a private variable to count the number of cubes that we instantiate.
Code inside here is executed when the mouse hovers over a game object which contains a GUIElement or a Collider. // Fades the red component of the material to zero // while the mouse is over the mesh function OnMouseOver () { renderer.material.color.r -= 0.1 * Time.deltaTime; } Check the Unity API for more information on all of these functions.