Physics-in-Unity
Physics-in-Unity
ENGINES
UNITY 3D
PHYSICS IN UNITY
1
Mobile Game Engines
So many ; Which one to use?
Corona SDK
By corona LABS : first released in 2009
Princing: free
Supported Languages: LUA
Characteristics:
Over 1000 API’s
Vast Plugin Library
- : a limited number of platforms you can export to (Windows/ MAC)
2
Mobile Game Engines
So many ; Which one to use?
COCOS2D-X
Open source : first released in 2008
Princing: free
Supported Languages: C++, LUA, JS
Characteristics:
Easy to understand
Specialized for building 2D games
3
Mobile Game Engines
So many ; Which one to use?
GODOT
Open source : first released in 2018
Princing: free
Supported Languages: C#, C++, JS
Characteristics:
2D and 3D cross platform game development
4
Mobile Game Engines
So many ; Which one to use?
GDEVELOP
Open source : first released in 2015
Princing: free
Supported Languages: No coding
Characteristics:
Visual Programming Interface
2D Specialized Engine
5
Mobile Game Engines
So many ; Which one to use?
Unity
Open source : first released in 2005
Princing: free(up to 125 $ monthly)
Supported Languages: C#
Characteristics:
Visual Programming Interface
2D/3D game dev Engine (well-known games developped with Unity: Mario, moment valley; 2D games Fruit
Bump,
Multi-platform
6
Unity interface composition
7
Unity interface composition
1. Scene View: This shows a view of your 3d world. In Unity,
the 3d world is called a “scene”.
2. Hierarchy View: This shows a list of all the objects in your
scene. In Unity, objects are called “game objects”.
3. Inspector View: This shows detailed information about the
currently selected game object.
4. Project View: This shows all the resources that your project
can make use of. This ranges
from source code to 3d objects, images, sounds, fonts, and other
files.
5. Toolbar: Various other buttons to interact with your project.
8
Runs when the game starts
9
Is called once per frame
10
wake: This function is always called before any Start functions and also just after a prefab
instantiated. (If a GameObject is inactive during start up Awake is not called until it is made active.)
or example, consider two different objects getting references to each others’ scripts and components…
n Awake, Object A gets a reference to one of its own components while Object B gets a reference to Obje
ObjectA:
11
wake: This function is always called before any Start functions and also just after a prefab
instantiated. (If a GameObject is inactive during start up Awake is not called until it is made active.)
or example, consider two different objects getting references to each others’ scripts and components…
n Awake, Object A gets a reference to one of its own components while Object B gets a reference to Obje
ObjectB:
12
Awake: This function is always called before any Start functions and also just after a prefab
is instantiated. (If a GameObject is inactive during start up Awake is not called until it is made activ
OnEnable: (only called if the Object is active): This function is called just after the object is enabled
Reset: is called when the script is attached and not in playmode
•Reset: Reset is called to initialize the script’s properties when it is first attached to
an object and also when the Reset command is used.
•OnValidate: OnValidate is called whenever the script’s properties are set,
including when an object is deserialized, which can occur at various times,
such as when you open a scene in the Editor and after a domain reload.
13
Update, FixedUpdate, LateUpdate
When you’re keeping track of game logic and interactions, animations,
camera
positions, etc., there are a few different events you can use. The common
pattern is to perform most tasks inside the Update function, but there
are also other functions you can use.
•FixedUpdate
- is often called more frequently than Update.
- It can be called multiple times per frame, if the frame rate is low and it
may not be called between frames at all if the frame rate is high.
- All physics calculations and updates occur immediately after
FixedUpdate.
- When applying movement calculations inside FixedUpdate, you do
not need to multiply your values by Time.deltaTime. This is because
FixedUpdate is called on a reliable timer, independent of the frame
rate.
(e.g :physics manipulation FixedUpdate
we want collision between two objects but they don’t have the same
speed
14
Update, FixedUpdate, LateUpdate
•LateUpdate:
- is called once per frame, after Update has finished.
- Any calculations that are performed in Update will have completed
when LateUpdate begins.
- A common use for LateUpdate would be a following third-person
camera. If you make your character move and turn inside Update,
you can perform all camera movement and rotation calculations
in LateUpdate. This will ensure that the character has moved
completely before the camera tracks its position.
15
OnEnable vs Start
On Enable and Start both get called when the script component is
enabled, whether that’s manually from a script or automatically at the
beginning of the game, with On Enable getting called first.
While Start will only ever be called once, On Enable is called every time
the script component, or the object it’s attached to, is enabled.
16
Coroutine: allow you to execute game logic over a number of frames.
17
Coroutine: allow you to execute game logic over a number of frames.
Example :
Let’s say I have a tank, and when I click on the map I want that tank to
turn to face where I clicked, move towards the position and, when it gets
there, wait 1 second before firing.
19
20
The code when using coroutine
21
Example 2: delay start in Unity
>It’s worth considering using a Coroutine whenever you want to create an action that needs to pause,
perform a series of steps in sequence or if you want to run a task
hat you know will take longer than a single frame.
StopAllCoroutines (): If many coroutines are actif and we want to stop them
23
Introduction To Game Objects
■ The place where you put game objects is called a scene. You can have
multiple scenes in your
■ game, for example, one to hold the main menu, and several others for the
levels or stages of your game.
24
Creating To Game Objects
■ In the top menu, choose GameObject > Create Other > Cube. This will create
a default cube at the center ofyour vision in the Scene View. You'll also find in
that menu other default objects
■ that you can create, but we'll concentrate on the cube for now.
25
Moving To Game Objects
27
Scaling Game Objects
you can scale game objects to do resizing, enlarging,
squashing, flattening and the like.
28
Selecting multiple Game Objects
29
Renaming multiple Game Objects
There are two ways to change the names of your Game Objects.
30
Renaming multiple Game Objects
31
Parenting multiple Game Objects
Parenting is Unity's process of grouping many game objects together.
1. Select the game object that you want to be grouped inside another one.
This will be the child.
32
Parenting multiple Game Objects
Parenting is Unity's process of grouping many game objects together.
2. Drag it to the other game object that will become the parent.
33
Parenting multiple Game Objects
Parenting is Unity's process of grouping many game objects together.
3. After that, the dragged game object is now parented to the other game object.
Parented game objects follow the parent's position, rotation, and scale.
34
Duplicating Game Objects
To create an exact copy of a game object, press Ctrl+D.
The duplicate will have the same scale and rotation as the original.
Your duplicate will also end up occupying the same space of the original object, so just move it
afterwards.
35
Rigidbody
■ allows your GameObjects to act under the control of physics.
■ allows you to interact with the physics of your objects and visualize
how Unity is trying to simulate the physics of the real world.
36
Rigidbody.useGravity
■ Controls whether gravity affects this rigidbody.
■ If set to false the rigidbody will behave as in outer space.
37
Rigidbody.mass
■ This property of the Rigidbody is used for defining the mass of your
object.
38
Rigidbody.drag
■ Drag can be interpreted as the amount of air resistance that affects
the object when moving from forces. It can be used for example to
slow down an object.
39
Rigidbody.isKinematic
■ When the Is Kinematic property is enabled, the object will no longer
be driven by the physics engine. Forces, joints, or collisions will stop
having an impact on the Rigibody. In this state, it can only be
manipulated by its Transform..
40
Rigidbody
.OnCollisionEnter(Collision)
■ How to use it
void OnCollisionEnter(Collision nom_de_la_collision) {
//traitement
}
■ OnCollisionEnter is called when this collider/rigidbody has begun
touching another rigidbody/collider.
41
Rigidbody
.OnCollisionEnter(Collision)
Example
42
Exercice
Create a simple collection of game objects looking like the picture below.
Make sure to parent all the cylinders to the box.
43
Introduction To Unity Components
In Unity, you can give your game objects any kind of properties. These properties are called
components. Components are the things that define what a game object is: what it has, and
how it behaves.
You've already encountered one of them: the “transform” component. This transform component defines the position,
rotation, and scale of a game object.
If you select a game object, the Inspector view will show all the components the selected game object has.
To display a 3d shape, your game object also needs other components. To define its shape to the physics engine, it also
needs another different type of component.
In the game objects you've encountered, the component “Mesh Filter” specifies which 3d shape the game object uses. The
component “Mesh Renderer” takes care of displaying that 3d shape. “Box Collider” specifies its shape (as a box) to the
physics engine.
Interacting With The Physics: The Rigidbody Component
Try this: In the Lesson 1 project, locate the game object called “Simple Cylinder”. Select it. Focus on it by pressing F.
Add a rigidbody component by going to the top menu and choosing Component > Physics > Rigidbody.
44
Now in the toolbar, you will see three buttons in the middle
like the picture to the left. These buttons control when to start
running your game.
The leftmost button starts the game. The middle button pauses your game. And the
rightmost button lets you advance the game by one step at a time.
Go ahead and press the leftmost button to start your game. When your game is being run,
Unity calls this “Play Mode”.
If you did everything correctly, you'll see the cylinder fall to the ground like a real world
object. This is what the rigidbody component does: it makes your game object
react to the physics.
Now, to stop your game from running, click on the play button again. When the game is not
running, Unity calls this “Edit Mode”.
45