The using keyword provides packages for us to use for free while building our project.
Methods are stored in classes and then called with a callback function.
void Start is called when we start the game.
void Update is called for every single frame of the game (after the game is started).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Driver : MonoBehavior
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
Rotates the game object to a certain position once.
// Start is called before the first frame update
void Start()
{
transform.Rotate(0, 0, 45);
}
Will continue to rotate every frame per second.
(If the number is a decimal, put the f keyword at the end for float).
// Update is called once per frame
void Update()
{
transform.Rotate(0, 0, 0.1f);
}
Moves the game object either forward or backward.
// Update is called once per frame
void Update()
{
transform.Translate(0, 0.01f, 0);
}
Combining both rotate and translate method allows the game object to move forward/backward while rotating at the same time.
// Update is called once per frame
void Update()
{
transform.Rotate(0, 0, 0.1f);
transform.Translate(0, 0.01f, 0);
}
// Example
int hitPoints = 20; // int refers to numbers
float speed = 3.8f; // write 'f' at the end for floats
bool isFun = true;
string myName = John;
Write variables within classes and outside the Start() and Update() functions.
public class Driver : MonoBehavior
{
float steerSpeed = 0.1f;
float moveSpeed = 0.01f;
void Start()
{
}
void Update()
{
transform.Rotate(0, 0, steerSpeed);
transform.Translate(0, moveSpeed, 0);
}
}
SerializeField is an attribute that you can use to force a private field to be visible in the Unity Editor's Inspector without making it public. This way, you can still adhere to good object-oriented practices by keeping fields private, while still being able to set their values from the Editor.
Using SerializeField allows us to directly test our code on the Unity Editor visually.
[SerializeField]
// Example
public class Driver : MonoBehavior
{
[SerializeField] float steerSpeed = 0.1f;
[SerializeField] float moveSpeed = 0.01f;
void Start()
{
}
void Update()
{
transform.Rotate(0, 0, steerSpeed);
transform.Translate(0, moveSpeed, 0);
}
}
To implement an input system, we need to use the Input class and select the type of input system we want to use (ex. keyboard/mouse, joystick).
We put our input variable within the update method because the amount of frame needs to be updated constantly.
If you are using the "old" system, make sure to specifically use the exact names.
void Update()
{
float steerAmount = Input.GetAxis("Horizontal") * steerSpeed;
float moveAmount = Input.GetAxis("Vertical") * moveSpeed;
transform.Rotate(0, 0, -steerAmount);
transform.Translate(0, moveAmount, 0);
}
Depending on the specs of our computer, our game object can move much faster or slower.
We simply need to multiply our speed by the **Time.deltaTime
void Update()
{
float steerAmount = Input.GetAxis("Horizontal") * steerSpeed * Time.deltaTime;
float moveAmount = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
transform.Rotate(0, 0, -steerAmount);
transform.Translate(0, moveAmount, 0);
}
Colliders are components attached to GameObjects in Unity that define their physical shape for the purposes of collision detection and physics interactions. They are used to determine whether two objects are touching or intersecting in the game world. Colliders come in various shapes, such as boxes, spheres, capsules, and more complex mesh-based colliders.
Unity provides several types of colliders, including:
Rigidbodies are components that are used to simulate physical behavior and movement in Unity's physics system. When you attach a Rigidbody component to a GameObject, that GameObject becomes subject to Unity's physics simulations, including forces, gravity, and collisions. Rigidbodies allow objects to be affected by physics forces like gravity, be pushed or pulled by other objects, and react realistically to collisions.
Rigidbodies have various properties and settings that can be adjusted, such as mass, drag, angular drag, and constraints on movement and rotation. By manipulating these properties and applying forces or torque to rigidbodies, you can create a wide range of dynamic and interactive behaviors within your game.
OnCollisionEnter2D() is a method in the Unity game engine that is used in 2D physics-based games to detect collisions between two game objects with Collider2D components.
With this method, 2D sprites will react as soon as they come into contact with another game object.
// Example: The game object reacts with a message
public class Collision : MonoBehaviour
{
void OnCollisionEnter2D(Collision2D other)
{
Debug.Log("Ouch!");
}
}
OnTriggerEnter2D is a method in Unity's MonoBehaviour class that gets called when a 2D collider enters a trigger collider. It's commonly used in 2D games to detect when two colliders intersect in a way that is set up as a trigger interaction.
Ex. Power ups in Super Mario Bros., hearts in the Legend of Zelda.
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("What was that?!?");
}
In Unity, assets refer to the various types of files and resources that are used to create and build your game or interactive experience. Assets are the building blocks of your Unity project, and they can encompass a wide range of elements, including:
3D Models: These are representations of objects in a 3D space. They can include characters, props, environments, and more.
2D Sprites and Textures: These are images used for characters, backgrounds, user interface elements, and more.
Animations: Animation assets contain keyframes and data for creating dynamic movement and behavior for characters and objects.
Audio Clips: These assets store audio data for sound effects, music, voiceovers, and other auditory elements.
Scripts: Unity uses scripts written in languages like C# to control the behavior and functionality of objects in your game.
Materials and Shaders: Materials define how an object's surface should appear, and shaders control how light interacts with those materials.
Prefabs: Prefabs are templates for GameObjects that can be reused throughout your project. They allow you to create instances of a GameObject with predefined properties and components.
Scenes: Scenes are individual levels or sections of your game. They consist of a collection of GameObjects, assets, and other settings that define a specific part of your game.
Particle Systems: Particle systems create dynamic effects like smoke, fire, explosions, and more.
Physics Materials: These materials define how objects interact with each other physically, such as friction and bounciness.
Animations: Animation assets are used to create animations for characters, objects, and other elements in your game.
Fonts: Font assets define the appearance of text in your game's user interface.
Terrain Data: If your game uses terrains, this asset type contains heightmaps and other data that define the landscape.
Video Files: Video assets can be used for cutscenes or interactive video elements in your project.
Assets are organized within the Unity project's Assets folder and can be imported, managed, and manipulated through the Unity Editor. Unity provides a wide range of tools to work with these assets, allowing developers and designers to create complex and engaging interactive experiences.
Resolution refers to the number of pixels in an image.
Higher resolution = more pixels
New assets default to 100 pixels per Unity unit.
Bigger: less pixels per unit