Download presentation
Presentation is loading. Please wait.
Published byJemimah Hutchinson Modified over 6 years ago
1
Unity3D for Mobile Sprites, Camera, Physics
SoftUni Team 2D Mode Technical Trainers Software University Sprites, Camera, Physics
2
Table of Contents
3
Unity3D for Mobile unity
4
Camera Modes and Settings
2D Camera View Unity3D for Mobile Camera Modes and Settings
5
Camera Projection - Perspective
Field of View - Changes the camera lens
6
Camera Projection - Perspective
FOV - 88 FOV - 55
7
Camera Projection - Orthographic
8
Creating and Using Sprites
Unity3D for Mobile Creating and Using Sprites
9
Sprites Single Multiple
Unity3D for Mobile Sprites Single Multiple The Sprite is a Texture Type for use with the 2D framework.
10
Using and Cutting Sprites
Live Demo Using and Cutting Sprites
11
2D Rigidbody and 2D Colliders
2D Collisions Unity3D for Mobile 2D Rigidbody and 2D Colliders
12
Unity3D for Mobile Mass The mass is given in arbitrary units but the basic physical principles of mass apply. From Newton's classic equation force = mass * acceleration, it is apparent that the larger an object's mass, the more force it requires to accelerate it to a given velocity. Also, mass affects momentum, which is significant during collisions; an object with large mass will be moved less by a collision than an object with lower mass.
13
Interpolate Unity3D for Mobile
Interpolation is used to estimate the position of the Rigidbody between physics updates. It can be useful to switch this on when the graphics update is much more frequent than the physics update because the object can appear to move along in jerky "hops" rather than having smooth motion. With interpolate mode, motion is smoothed based on the object's positions in previous frames. Extrapolate mode smooths motion based on an estimate of its position in the next frame. The choice of mode depends of the dynamics of the object during gameplay.
14
Constraints Unity3D for Mobile
Controls which degrees of freedom are allowed for the simulation of this Rigidbody2D. By default this is set to none, allowing rotation and movement along all axes. In some cases, you may want to constrain a Rigidbody2D to only move or rotate along some axes.
15
Velocity Unity3D for Mobile Linear velocity of the rigidbody.
The velocity is specified as a vector with components in the X and Y directions (there is no Z direction in 2D physics). The value is not usually set directly but rather by using forces. The velocity can also gradually decay due to the effect of drag if this is enabled.
16
2D Rigidbody Unity3D for Mobile Scripting
17
ForceMode2D.Force ForceMode2D.Impulse
Unity3D for Mobile ForceMode2D.Force Adds a force to the Rigidbody, using its mass. Adds an instant force impulse to the Rigidbody, using its mass. ForceMode2D.Impulse Force: Apply the force in each FixedUpdate over a duration of time. This mode depends on the mass of rigidbody so more force must be applied to move higher-mass objects the same amount as lower-mass objects. This mode is useful for setting up realistic physics where it takes more force to move heavier objects. Impulse: Apply the impulse force instantly. This mode depends on the mass of rigidbody so more force must be applied to move higher-mass objects the same amount as lower-mass objects. This mode is useful for applying forces that happen instantly, such as forces from explosions or collisions.
18
AddForce() - Force Unity3D for Mobile Apply a force to the rigidbody.
this.rigidbody2D = this.GetComponent<Rigidbody2D>(); Vector2 nextPos = new Vector2(5,0) + this.rigidbody2D.position * Time.deltaTime; this.rigidbody2D.AddForce(nextPos, ForceMode2D.Force); Apply a force to the rigidbody. The force is specified as two separate components in the X and Y directions (there is no Z direction in 2D physics). The object will be accelerated by the force according to the law force = mass * acceleration - the larger the mass, the greater the force required to accelerate to a given speed.
19
AddForce() - Impulse Unity3D for Mobile
this.rigidbody2D = this.GetComponent<Rigidbody2D>(); Vector2 explosionForce = new Vector2(5,0); this.rigidbody2D.AddForce(explosionForce, ForceMode2D.Impulse); Apply a force to the rigidbody. The force is specified as two separate components in the X and Y directions (there is no Z direction in 2D physics). The object will be accelerated by the force according to the law force = mass * acceleration - the larger the mass, the greater the force required to accelerate to a given speed.
20
AddTorque() Unity3D for Mobile
this.rigidbody2D.AddTorque(5, ForceMode2D.Impulse); Apply a torque at the rigidbody's center of mass. A torque is conceptually a force being applied at the end of an imaginary lever, with the fulcrum at the center of mass. A torque of five units could thus be equivalent to a force of five units pushing on the end of a lever one unit long, or a force of one unit on a lever five units long. Unity's units are arbitrary but the principle that torque = force * lever length still applies. Torque can be used to simulate car engine. Note that unlike a 3D Rigidbody, a Rigidbody2D can only rotate in one axis and so torque is a float value rather than a vector.
21
AddForceAtPosition()
Unity3D for Mobile AddForceAtPosition() Vector2 explosionPosition = new Vector2(-7, -2); Vector2 explosion = new Vector2(8, 8); this.rigidbody2D.AddForceAtPosition(explosion, explosionPosition, ForceMode2D.Impulse); Apply a force at a given position in space. The AddForce() method applies a force that acts straight through the rigidbody's center of mass and so produces only positional movement and no rotation. AddForceAtPosition() can apply the force at any position in world space and will typically also apply a torque to the object which will set it rotating. Note that for the purposes of this function, the rigidbody is just a coordinate space of infinite size, so there is no reason why the force needs to be applied within the confines of the object's graphic or colliders.
22
MovePosition() Unity3D for Mobile
this.rigidbody2D.MovePosition(new Vector2(6.5f, 0)); this.rigidbody2D.MovePosition(new Vector2(1500, 0)); Moves the rigidbody to position. Moves the rigidbody to the specified position by calculating the appropriate linear velocity required to move the rigidbody to that position during the next physics update. During the move, neither gravity or linear drag will affect the body. This causes the object to rapidly move from the existing position, through the world, to the specified position.
23
MoveRotation() this.rigidbody2D.MoveRotation(65); Unity3D for Mobile
Rotates the rigidbody to angle (given in degrees). Rotates the rigidbody to the specified angle by calculating the appropriate angular velocity required to rotate the rigidbody to that angle during the next physics update. During the move, angular drag won't affect the body. This causes the object to rapidly move from the existing angle to the specified angle.
24
Sleep() WakeUp() IsSleeping() Unity3D for Mobile Sleep():
Sleeping is an optimization that is used to temporarily remove an object from physics simulation when it is at rest. This function makes the rigidbody sleep. WakeUp(): Disables the "sleeping" state of a rigidbody. IsSleeping(): Checks if the rigidbody is sleeping.
25
2D Colliders Properties in Editor Unity3D for Mobile
Collider components define the shape of an object for the purposes of physical collisions. A collider, which is invisible, does not need to be the exact same shape as the object’s mesh and in fact, a rough approximation is often more efficient and indistinguishable in gameplay.
26
Is Trigger Unity3D for Mobile Is the collider a trigger?
A trigger doesn't register a collision with an incoming Rigidbody. Instead, it sends OnTriggerEnter, OnTriggerExit and OnTriggerStay message when a rigidbody enters or exits the trigger volume.
27
Used by Effector Unity3D for Mobile
Whether the collider is used by an attached effector or not. When checked, the Collider2D continues to work as a collision or trigger area however it will also be used by any Effector2D on the same GameObject.
28
BoxCollider2D Collider representing an axis-aligned rectangle.
Unity3D for Mobile BoxCollider2D Collider representing an axis-aligned rectangle.
29
CircleCollider2D Unity3D for Mobile
Its very similar to the BoxCollider2D
30
PolygonCollider2D Unity3D for Mobile
Collider representing an arbitrary polygon defined by its vertices. The collider’s shape is defined by a freeform edge made of line segments, so you can adjust it to fit the shape of the Sprite graphic with great precision. Note that this collider’s edge must completely enclose an area (unlike the similar EdgeCollider2D).
31
EdgeCollider2D Unity3D for Mobile
Collider representing an arbitrary set of connected edges (lines) defined by its vertices. Note that this collider’s edge doesn’t need to completely enclose an area (unlike the similar PolygonCollider2D) and can be as simple as a straight line or an L-shape.
32
2D Colliders Unity3D for Mobile Scripting
33
On Collision Enter void OnCollisionEnter2D(Collision2D collision2D) {
Unity3D for Mobile On Collision Enter void OnCollisionEnter2D(Collision2D collision2D) { // Your Code } Sent when an incoming collider makes contact with this object's collider. Further information about the collision is reported in the Collision2D parameter passed during the call. If you don't need this information then you can declare OnCollisionEnter2D without the parameter. Notes: Collision events will be sent to disabled MonoBehaviours, to allow enabling Behaviors in response to collisions.
34
On Collision Stay void OnCollisionStay2D(Collision2D collision2D) {
Unity3D for Mobile On Collision Stay void OnCollisionStay2D(Collision2D collision2D) { // Your Code } Sent each frame where a collider on another object is touching this object's collider
35
On Collision Exit void OnCollisionExit2D(Collision2D collision2D) {
Unity3D for Mobile On Collision Exit void OnCollisionExit2D(Collision2D collision2D) { // Your Code } Sent when a collider on another object stops touching this object's collider.
36
On Trigger Enter void OnTriggerEnter2D(Collider2D other) {
Unity3D for Mobile On Trigger Enter void OnTriggerEnter2D(Collider2D other) { // Your Code } Sent when another object enters a trigger collider attached to this object. Further information about the other collider is reported in the Collider2D parameter passed during the call. Notes: Trigger events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions.
37
On Trigger Stay void OnTriggerStay2D(Collider2D other) { // Your Code
Unity3D for Mobile On Trigger Stay void OnTriggerStay2D(Collider2D other) { // Your Code } Sent each frame where another object is within a trigger collider attached to this object.
38
On Trigger Exit void OnTriggerExit2D(Collider2D other) { // Your Code
Unity3D for Mobile On Trigger Exit void OnTriggerExit2D(Collider2D other) { // Your Code } Sent when another object leaves a trigger collider attached to this object.
39
Tags, Layers, Collision Matrix
Unity3D for Mobile Properties in Editor Collider components define the shape of an object for the purposes of physical collisions. A collider, which is invisible, does not need to be the exact same shape as the object’s mesh and in fact, a rough approximation is often more efficient and indistinguishable in gameplay.
40
Unity3D for Mobile Tags A Tag is a word which you link to one or more GameObjects. For instance, you might define “Player” and “Enemy” Tags for player-controlled characters and non-player characters respectively; a “Collectable” Tag could be defined for items the player can collect in the Scene; and so on. Clearly, Tags are intended to identify GameObjects for scripting purposes. We can use them to write script code to find a GameObject by looking for any object that contains our desired Tag. This is achieved using the GameObject.FindWithTag() function.
41
Layers Unity3D for Mobile
Layers are most commonly used by Cameras to render only a part of the scene, and by Lights to illuminate only parts of the scene. But they can also be used by raycasting to selectively ignore colliders or to create collisions.
42
Using Layers Unity3D for Mobile
Here we make everything using the “Invisible Wall” layer invisible for the camera.
43
Collision Matrix Unity3D for Mobile
Layer-based collision detection is a way to make a GameObject collide with another GameObject that is set up to a specific Layer or Layers. Using the Collision Matrix you can tell you game that the player won’t collide with lasers, but everything else will collide with them.
44
Using Collision Matrix
Unity3D for Mobile Using Collision Matrix In this example we are disabling the collisions between our “Enemies” and “Water”, and “Invisible Wall” layers. This means that collision will not be detected between those layers.
45
Tags Unity3D for Mobile Scripting
46
Using Tags Unity3D for Mobile
void OnCollisionEnter2D(Collision2D collision2D) { if (collision2D.transform.tag == "Laser") this.Health -= 10; }
47
15
48
Effectors Unity3D for Mobile Creating Platforms
49
Area Effector 2D Unity3D for Mobile
The Area Effector 2D applies forces within an area defined by the attached Collider 2Ds when another (target) Collider 2D comes into contact with the Effector 2D. You can configure the force at any angle with a specific magnitude and random variation on that magnitude. You can also apply both linear and angular drag forces to slow down Rigidbody 2Ds. Collider 2Ds that you use with the Area Effector 2D would typically be set as triggers, so that other Collider 2Ds can overlap with it to have forces applied. Non-triggers will still work, but forces will only be applied when Collider 2Ds come into contact with them.
50
Buoyancy Effector 2D Unity3D for Mobile
The Buoyancy Effector 2D defines simple fluid behavior such as floating and the drag and flow of fluid. You can also control a fluid surface, with the fluid behavior taking place below.
51
Platform Effector 2D Unity3D for Mobile
The Platform Effector 2D applies various “platform” behaviour such as one-way collisions, removal of side-friction/bounce etc. Colliders that you use with the effector would typically not be set as triggers so that other colliders can collide with it.
52
Surface Effector 2D Unity3D for Mobile
The Surface Effector 2D applies tangent forces along the surfaces of colliders used by the effector in an attempt to match a specified speed along the surface. This is analogous to a conveyor belt. Colliders that you use with the effector would typically be set as non-triggers so that other colliders can come into contact with the surface.
53
Point Effector 2D Unity3D for Mobile
The Point Effector 2D applies forces to attract/repulse against a source point which can be defined as the position of the rigid-body or the center of a collider used by the effector. When another (target) collider comes into contact with the effector then a force is applied to the target. Where the force is applied and how it is calculated can be controlled. Colliders that you use with the effector would typically be set as triggers so that other colliders can overlap with it to have forces applied however, non-triggers will still work but forces will only be applied when colliders come into contact with it.
54
Joints Unity3D for Mobile Attaching Objects
55
Distance Joint Unity3D for Mobile
Distance Joint 2D is a 2D joint that attaches two GameObjects controlled by Rigidbody 2D physics, and keeps them a certain distance apart.
56
Fixed Joint 2D Unity3D for Mobile
Apply this component to two GameObjects controlled by Rigidbody 2D physics to keep them in a position relative to each other, so the GameObjects are always offset at a given position and angle. It is a spring-type 2D joint for which you don’t need to set maximum forces. You can set the spring to be rigid or soft.
57
Friction Joint 2D Unity3D for Mobile
The Friction Joint 2D connects GameObjects controlled by Rigidbody 2D physics. The Friction Joint 2D reduces both the linear and angular velocities between the objects to zero (ie, it slows them down). You can use this joint to simulate top-down friction, for example.
58
Hinge Joint 2D Unity3D for Mobile
The Hinge Joint 2D component allows a GameObject controlled by RigidBody 2D physics to be attached to a point in space around which it can rotate. The rotation can be left to happen passively (for example, in response to a collision) or can be actively powered by a motor torque provided by the Joint 2D itself. You can set limits to prevent the hinge from making a full rotation, or make more than a single rotation.
59
Relative Joint 2D Unity3D for Mobile
This joint component allows two game objects controlled by rigidbody physics to maintain in a position based on each other’s location. Use this joint to keep two objects offset from each other, at a position and angle you decide.
60
Slider Joint 2D Unity3D for Mobile
This joint allows a game object controlled by rigidbody physics to slide along a line in space, like sliding doors, for example. The object can freely move anywhere along the line in response to collisions or forces or, alternatively, it can be moved along by a motor force, with limits applied to keep its position within a certain section of the line.
61
Spring Joint 2D Unity3D for Mobile
The Spring Joint 2D component allows two game objects controlled by rigidbody physics to be attached together as if by a spring. The spring will apply a force along its axis between the two objects, attempting to keep them a certain distance apart.
62
Target Joint 2D Unity3D for Mobile
This joint connects to a specified target, rather than another rigid body object, as other joints do. It is a spring type joint, which you could use for picking up and moving an object acting under gravity, for example.
63
Wheel Joint 2D Unity3D for Mobile
Use the Wheel Joint 2D to simulate a rolling wheel, on which an object can move. You can apply motor power to the joint. The wheel uses a suspension “spring” to maintain its distance from the main body of the vehicle.
64
Lecture Name 2D Mode - Summary 2D collisions work almost the same as their 3D counterparts There are many available 2D effectors and joints that will make your game better!
65
Unity3D for Mobile
66
Unity3D for Mobile
67
Unity3D for Mobile
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.