Install Unity: https://unity.com/download
Quickstart: Configure Visual Studio for cross-platform development with Unity (Microsoft) - https://learn.microsoft.com/en-us/visualstudio/gamedev/unity/get-started/getting-started-with-visual-studio-tools-for-unity?pivots=windows
C# Language
C# programming guide (Microsoft) - https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/
C# Tutorial (W3Schools) - https://www.w3schools.com/cs/index.php
Some Obvious Syntax Differences in C# from JAVA:
methods are in PascalCase instead of camelCase.
enhanced for loop is spelled as foreach
Unity API Reference
Input Class - https://docs.unity3d.com/ScriptReference/Input.html
Vector2 - https://docs.unity3d.com/ScriptReference/Vector2.html
Rigidbody2D - https://docs.unity3d.com/ScriptReference/Rigidbody2D.html
Rigidbody2D.AddForce - https://docs.unity3d.com/ScriptReference/Rigidbody2D.AddForce.html
Rigidbody2D.velocity - https://docs.unity3d.com/ScriptReference/Rigidbody2D-velocity.html
To prevent a Rigidbody2D from rotating. Freeze Rotation in inspector
Component.GetComponent - https://docs.unity3d.com/ScriptReference/Component.GetComponent.html
Object.Destroy - https://docs.unity3d.com/ScriptReference/Object.Destroy.html
2D Project
Game perspectives for 2D games - https://docs.unity3d.com/Manual/Quickstart2DPerspective.html
Import Assets. Assets -> Import New Assets...
Add RigidBody 2D. First select the asset, then choose Component -> Add...
You will find the GameObject now will fall when you run, due to gravity.
Add a floor. GameObject -> Sprites -> Square, then sketch it. When you run, you will see that it fell through.
Select each object, choose Component -> Physics 2D -> Box Collider 2D
Add Script. Assets -> Create -> C# Script
Use the following code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Your_Script_Name: MonoBehaviour
{
Rigidbody2D obj1;
// Start is called before the first frame update
void Start()
{
obj1 = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.UpArrow))
{
obj1.AddForce(new Vector2(0,300));
}else if(Input.GetKey(KeyCode.LeftArrow))
{
obj1.AddForce(new Vector2(-3,0));
}else if(Input.GetKey(KeyCode.RightArrow))
{
obj1.AddForce(new Vector2(3,0));
}
}
}
Drag the script to the object you want to apply
Tag
Prevent mid air jump.
Add tag "Ground" to grounds. Click on the object; click Tag; Add an "Ground" tag.
Add a bool variable
bool isGrounded = false;
When touch ground
void OnCollisionEnter2D(Collision2D other){
if (other.gameObject.tag == "Ground"){
isGrounded = true;
}
}
Check for ground
void OnCollisionEnter2D(Collision2D other){
if (other.gameObject.tag == "Ground"){
isGrounded = true;
}
}
Destroying a collided gameObject with a tag of "Gem"
To allowed two Collider objects to pass through each other yet be able to detect the collision, click and check the isTrigger checkbox in the Collider component.
void OnTriggerEnter2D(Collider2D other){
if (other.gameObject.tag == "Gem"){
Destroy(other.gameObject);
}
}
Camera
Drag the Main Camera and drop it on top the player.
In the inspection window, change transform to x= 0, y=0 so that the camera is centered on the player.
Text
Text - https://docs.unity3d.com/2018.2/Documentation/ScriptReference/UI.Text.html
import
using UnityEngine.UI
use a variable to hold the Text
Text t;
store the Text component to the variable
t = GameObject.Find("Text_object_name").GetComponent<Text>();
change the text
t.text = "Something";
Animation
Window -> Animation -> Animation
Introduction to Sprite Animations (Unity Learning) - https://learn.unity.com/tutorial/introduction-to-sprite-animations
Animator Controllers (Unity Learning) - https://learn.unity.com/tutorial/animator-controllers
Animation Parameters (Unity Learning) - https://docs.unity3d.com/Manual/AnimationParameters.html
Animator anim;
anim = GetComponent<Animator>();
anim.SetInteger(int id, float value);
anim.SetFloat(int id, float value);
anim.SetBool(int id, float value);
anim.SetTrigger(int id);
Unity Build Settings - https://docs.unity3d.com/Manual/BuildSettings.html
SceneManager - https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.html
SceneManager.LoadScene - https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html