PRESENTER
Enrico BARACAGLIA
WORKSHOP #2
Beginners
Introduction to
Unity3D and C#
29 AUGUST 2018
ORGANIZED BY
ESO, Chile
SUPERVISOR
Frédéric VOGT
Overview
1. What is Unity?
2. What is C#?
3. Object Oriented
Programming in C#
4. Writing C# code in Unity
5. Guided practical session
a. Make your first scene
b. Transform.Rotate
c. Transform.Translate
2
What is
Unity?
329/08/18 | enricobaracaglia@gmail.com
Unity is a cross-platform game development
engine used to build high-quality 2D, 3D, VR and
AR apps [1].
[1] https://unity3d.com/
[2]
https://store.unity.com/products/unity
-personal
From [2]
What is C#?
429/08/18 | enricobaracaglia@gmail.com
C# is a simple, modern, general-purpose, object-
oriented programming language developed by
Microsoft.
Reasons that make C# a widely used professional language:
- It is a modern, general-purpose programming language
- It is object oriented
- It is easy to learn
- It is a structured language
- It produces efficient programs
- It can be compiled on a variety of computer platforms
- It is a part of .Net Framework.
Object
Oriented
Programming
in C#
529/08/18 | enricobaracaglia@gmail.com
Define a class:
class MyClass
{
//insert code here
}
Define a structure:
struct MyStructure
{
//insert code here
}
Define properties:
class Car
{
public float fuel;
private int maxspeed;
}
Object Oriented Programming
https://docs.microsoft.com/en-
us/dotnet/csharp/programming-
guide/concepts/object-oriented-
programming
629/08/18 | enricobaracaglia@gmail.com
Object Oriented Programming
https://docs.microsoft.com/en-
us/dotnet/csharp/programming-
guide/concepts/object-oriented-
programming
A method is an action that an object can perform.
To define a method of a class:
class Car
{
public float fuel;
private int maxspeed;
public void PrintFuelLevel ( )
{
// Insert code here
}
}
Object
Oriented
Programming
in C#
729/08/18 | enricobaracaglia@gmail.com
A method is an action that an object can perform.
To define a method of a class:
class Car
{
public float fuel;
private int maxspeed;
public void PrintFuelLevel ( )
{
print (fuel);
}
}
public The variable can be accessed by any other code in
the same assembly or another assembly that references it.
private The variable can only be accessed by code in the same class.
Object Oriented Programming
https://docs.microsoft.com/en-
us/dotnet/csharp/programming-
guide/concepts/object-oriented-
programming
Object
Oriented
Programming
in C#
829/08/18 | enricobaracaglia@gmail.com
A method is an action that an object can perform.
To define a method of a class:
class Car
{
public float fuel;
private int maxspeed;
public void PrintFuelLevel ( )
{
print (fuel);
}
}
How to access the variable of a class?
Car.fuel
Object Oriented Programming
https://docs.microsoft.com/en-
us/dotnet/csharp/programming-
guide/concepts/object-oriented-
programming
Object
Oriented
Programming
in C#
Writing C#
code in Unity
9
Unity → Start:
public void Start
{
}
Unity → Update:
public void Update
{
}
[3]
https://unity3d.com/learn/tutorials/topi
cs/scripting/variables-and-functions
using UnityEngine;
public class VariablesAndFunctions : MonoBehaviour
{
public int myInteger = 5;
public void Start ( )
{
MultiplyByTwo( );
}
public void MultiplyByTwo ( )
{
float ret = myInteger * 2;
print (ret);
}
}
Writing C#
code in Unity
10
Unity → Start:
public void Start
{
}
Unity → Update:
public void Update
{
}
[3]
https://unity3d.com/learn/tutorials/topi
cs/scripting/variables-and-functions
using UnityEngine;
public class VariablesAndFunctions : MonoBehaviour
{
public int myInteger = 5;
public void Update ( )
{
MultiplyByTwo( );
}
public void MultiplyByTwo ( )
{
float ret = myInteger * 2;
print (ret);
}
}
Guided
practical
session
1129/08/18 | enricobaracaglia@gmail.com
Guided
practical
session
Make your
first scene
1229/08/18 | enricobaracaglia@gmail.com
STEPS:
1) Open Unity
2) Open the given Project
3) Open the scene named: Workshop2
4) Create a Plane and a Cube in the Scene
Guided
practical
session
Make your
first scene
1329/08/18 | enricobaracaglia@gmail.com
STEPS:
5) Create a new Folder to store your C# script
6) Create a C# script and name it MoveObject.
This will be our first ‘class’.
7) Copy and paste the given code inside the class
Important note: the name of the C# script has to be
always equal to the name of the class
public GameObject cube;
//ROTATE
public float rotationalSpeed;
public float rotateHorizontal;
public float rotateVertical;
// Use this for initialization
void Start ()
{
rotationalSpeed = 2.0f;
}
// Update is called once per frame
void Update ()
{
rotateHorizontal = Input.GetAxis("Horizontal");
rotateVertical = Input.GetAxis("Vertical");
if (rotateHorizontal != 0 || rotateVertical != 0)
{
//add code here
}
}
public void RotateCube()
{
cube.transform.Rotate(Vector3.up, rotateHorizontal * rotationalSpeed, Space.World);
cube.transform.Rotate(Vector3.left, rotateVertical * rotationalSpeed, Space.World);
}
Guided
practical
session
Rotate Cube
14
Transform.Rotate
https://docs.unity3d.com/ScriptRefere
nce/Transform.Rotate.html
Guided
practical
session
Translate
Cube
1529/08/18 | enricobaracaglia@gmail.com
What if we use our model instead of
the cube?
Guided
practical
session
Translate
Cube
1629/08/18 | enricobaracaglia@gmail.com
How can we make the cube move?
Guided
practical
session
Translate
Cube
1729/08/18 | enricobaracaglia@gmail.com
How can we make the cube move?
public void MoveCube()
{
cube.transform.Translate(Vector3.up * motionSpeed * moveVertical , Space.World);
cube.transform.Translate(Vector3.right * motionSpeed * moveHorizontal, Space.World);
}
Transform.Translate
https://docs.unity3d.com/ScriptRefere
nce/Transform.Translate.html
moveHorizontal = - Input.GetAxis("Horizontal");
moveVertical = Input.GetAxis("Vertical");
if (moveHorizontal != 0 || moveVertical != 0)
{
MoveCube();
}
Summary
18
In this workshop you learned how to:
✓ Interact with the Engine Game Unity3D and
understand simple C# code
✓ Use Transform.Rotate to rotate an object in
Unity
✓ Use Transform.Translate to move an
object in Unity
“
Thanks for listening.
Any questions?
1929/08/18 | enricobaracaglia@gmail.com https://www.linkedin.com/in/enrico-baracaglia-978789100/

VR Workshop #2

  • 1.
    PRESENTER Enrico BARACAGLIA WORKSHOP #2 Beginners Introductionto Unity3D and C# 29 AUGUST 2018 ORGANIZED BY ESO, Chile SUPERVISOR Frédéric VOGT
  • 2.
    Overview 1. What isUnity? 2. What is C#? 3. Object Oriented Programming in C# 4. Writing C# code in Unity 5. Guided practical session a. Make your first scene b. Transform.Rotate c. Transform.Translate 2
  • 3.
    What is Unity? 329/08/18 |enricobaracaglia@gmail.com Unity is a cross-platform game development engine used to build high-quality 2D, 3D, VR and AR apps [1]. [1] https://unity3d.com/ [2] https://store.unity.com/products/unity -personal From [2]
  • 4.
    What is C#? 429/08/18| enricobaracaglia@gmail.com C# is a simple, modern, general-purpose, object- oriented programming language developed by Microsoft. Reasons that make C# a widely used professional language: - It is a modern, general-purpose programming language - It is object oriented - It is easy to learn - It is a structured language - It produces efficient programs - It can be compiled on a variety of computer platforms - It is a part of .Net Framework.
  • 5.
    Object Oriented Programming in C# 529/08/18 |enricobaracaglia@gmail.com Define a class: class MyClass { //insert code here } Define a structure: struct MyStructure { //insert code here } Define properties: class Car { public float fuel; private int maxspeed; } Object Oriented Programming https://docs.microsoft.com/en- us/dotnet/csharp/programming- guide/concepts/object-oriented- programming
  • 6.
    629/08/18 | enricobaracaglia@gmail.com ObjectOriented Programming https://docs.microsoft.com/en- us/dotnet/csharp/programming- guide/concepts/object-oriented- programming A method is an action that an object can perform. To define a method of a class: class Car { public float fuel; private int maxspeed; public void PrintFuelLevel ( ) { // Insert code here } } Object Oriented Programming in C#
  • 7.
    729/08/18 | enricobaracaglia@gmail.com Amethod is an action that an object can perform. To define a method of a class: class Car { public float fuel; private int maxspeed; public void PrintFuelLevel ( ) { print (fuel); } } public The variable can be accessed by any other code in the same assembly or another assembly that references it. private The variable can only be accessed by code in the same class. Object Oriented Programming https://docs.microsoft.com/en- us/dotnet/csharp/programming- guide/concepts/object-oriented- programming Object Oriented Programming in C#
  • 8.
    829/08/18 | enricobaracaglia@gmail.com Amethod is an action that an object can perform. To define a method of a class: class Car { public float fuel; private int maxspeed; public void PrintFuelLevel ( ) { print (fuel); } } How to access the variable of a class? Car.fuel Object Oriented Programming https://docs.microsoft.com/en- us/dotnet/csharp/programming- guide/concepts/object-oriented- programming Object Oriented Programming in C#
  • 9.
    Writing C# code inUnity 9 Unity → Start: public void Start { } Unity → Update: public void Update { } [3] https://unity3d.com/learn/tutorials/topi cs/scripting/variables-and-functions using UnityEngine; public class VariablesAndFunctions : MonoBehaviour { public int myInteger = 5; public void Start ( ) { MultiplyByTwo( ); } public void MultiplyByTwo ( ) { float ret = myInteger * 2; print (ret); } }
  • 10.
    Writing C# code inUnity 10 Unity → Start: public void Start { } Unity → Update: public void Update { } [3] https://unity3d.com/learn/tutorials/topi cs/scripting/variables-and-functions using UnityEngine; public class VariablesAndFunctions : MonoBehaviour { public int myInteger = 5; public void Update ( ) { MultiplyByTwo( ); } public void MultiplyByTwo ( ) { float ret = myInteger * 2; print (ret); } }
  • 11.
  • 12.
    Guided practical session Make your first scene 1229/08/18| enricobaracaglia@gmail.com STEPS: 1) Open Unity 2) Open the given Project 3) Open the scene named: Workshop2 4) Create a Plane and a Cube in the Scene
  • 13.
    Guided practical session Make your first scene 1329/08/18| enricobaracaglia@gmail.com STEPS: 5) Create a new Folder to store your C# script 6) Create a C# script and name it MoveObject. This will be our first ‘class’. 7) Copy and paste the given code inside the class Important note: the name of the C# script has to be always equal to the name of the class
  • 14.
    public GameObject cube; //ROTATE publicfloat rotationalSpeed; public float rotateHorizontal; public float rotateVertical; // Use this for initialization void Start () { rotationalSpeed = 2.0f; } // Update is called once per frame void Update () { rotateHorizontal = Input.GetAxis("Horizontal"); rotateVertical = Input.GetAxis("Vertical"); if (rotateHorizontal != 0 || rotateVertical != 0) { //add code here } } public void RotateCube() { cube.transform.Rotate(Vector3.up, rotateHorizontal * rotationalSpeed, Space.World); cube.transform.Rotate(Vector3.left, rotateVertical * rotationalSpeed, Space.World); } Guided practical session Rotate Cube 14 Transform.Rotate https://docs.unity3d.com/ScriptRefere nce/Transform.Rotate.html
  • 15.
  • 16.
  • 17.
    Guided practical session Translate Cube 1729/08/18 | enricobaracaglia@gmail.com Howcan we make the cube move? public void MoveCube() { cube.transform.Translate(Vector3.up * motionSpeed * moveVertical , Space.World); cube.transform.Translate(Vector3.right * motionSpeed * moveHorizontal, Space.World); } Transform.Translate https://docs.unity3d.com/ScriptRefere nce/Transform.Translate.html moveHorizontal = - Input.GetAxis("Horizontal"); moveVertical = Input.GetAxis("Vertical"); if (moveHorizontal != 0 || moveVertical != 0) { MoveCube(); }
  • 18.
    Summary 18 In this workshopyou learned how to: ✓ Interact with the Engine Game Unity3D and understand simple C# code ✓ Use Transform.Rotate to rotate an object in Unity ✓ Use Transform.Translate to move an object in Unity
  • 19.
    “ Thanks for listening. Anyquestions? 1929/08/18 | enricobaracaglia@gmail.com https://www.linkedin.com/in/enrico-baracaglia-978789100/