36
Unity Physics

Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

Embed Size (px)

Citation preview

Page 1: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

Unity

Physics

Page 2: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

• You need to define the 8 vertices of

the box

• For Undergraduates you can make

one function for the entire cube

• For the graduate version, you’d be

best off making separate functions

for each face

• You need lists that will accumulate

all of the vertices, uvs, normal and

indices you generate as you iterate

through your 3D array

Homework Help

Page 3: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

• You need to iterate over the entire

volume of your 3D array, which will

require a triple for loop

• You will need to test if the value at that

location is a 0 or a 1 in your array

Homework Help

Page 4: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

Homework Help

0 1 1 1

1 1 0 1

1 1 1 1

0 1 1 1

0 1 1 1

1 1 0 1

1 1 1 1

0 1 1 1

0 1 1 1

1 1 0 1

1 1 1 1

0 1 1 1

Page 5: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

• To randomly fill an array with integer values we need to use

a different method than Unity’s version

• Use System.Random and set the range to 0 (inclusive) and 2

(exclusive)

Homework Help

Page 6: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

• When creating your cube, center your vertices around the origin

initially, add an offset based upon your (X,Y,Z) location later

• These initial positions never change and only by adding a Vector3

offset to each will we get our cube placed in the right location

• How do we know the offset? The triple for loop gives us the

offset!

Homework Help

p0

p1 p2

p3

p4

p5 p6

p7

Page 7: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

• We can expose serializable variables in our scripts through

the inspector in two ways:

• Make the variable public

• Declare the variable using the [SerializeField] attribute

More Unity 101

Page 8: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

• We can use the Physics Engine

in Unity if we want to simulate

forces and collisions

• When creating Primitive

Objects you may have noticed

they have Collider Components

attached

• Each is slightly different, but

there are several key similarities

More Unity 101 - Colliders

Page 9: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

• A collider is meant to detect

intersections

• It will interact with the physics

system and act as a boundary to

stop objects also controlled by the

physics system

• Is Trigger – if checked, this object

does not act as a boundary and

does not stop physics objects

• Two objects with only colliders on

them will not interact with each

other

• In order to make interactions occur,

another component is required:

Rigidbody

More Unity 101 - Colliders

Page 10: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Rigidbody

• Rigidbody controls the position of an object

through physics simulation

• Rigidbody needs to have a collider on it to

interact properly (otherwise things go crazy)

• Rigidbody allows you to apply forces that

make it react realistically

• Forces on Rigidbodies should only be

updated in FixedUpdate

• We can change the rate of FixedUpdate in:

Edit -> Project Settings -> Time

Page 11: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

• Create a plane and place a sphere

object some height above it

• Both objects have colliders, but if you

hit the play button nothing happens

More Unity 101 - Physics

• Add a Rigidbody component to the sphere and press play, the ball will

fall to the ground because the Rigidbody applies Gravity by default

• With Gravity turned off, place the ball inside the

plane and hit play

• You will notice the ball pops up or down out of the

plane because a collision was detected and it was

pushed away

• This will not happen if you omitted the rigid body

Page 12: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

• Rigidbodies require you to use forces

on the object, which can get messy

when trying to precisely control a

character or object

• ‘Is Kinematic’ option allows you to

modify the transform of the object

directly instead of using forces. It will

affect the motion of other

Rigidbodies, but will not be affected

by them

• Many times it is useful to switch

kinematic on and off over the lifetime

of an object to enable / disable

physics acting on it

More Unity 101 - Rigidbody

Page 13: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

• It is very possible as you develop your scripts you may require

a certain component be attached

• If you are working with forces to move the object, you will

need to make sure the object has a Collider and Rigid Body

• We can force Unity to check to make sure our object has these

components using the following attribute above your class

name:

[RequireComponent(typeof(Class))]

More Unity 101 – Requiring Components

Page 14: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

• Unity is able to inform us of a Trigger or Collision event

More Unity 101 – Triggers and Collisions

• We are notified when there is a strike to the surface,

while still within the boundary, and when it exits

• Let’s make an example of having ‘Lava’ damage a

character and regenerate health from a ‘Hospital’

Page 15: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

• Create a cylinder, sphere and box. Place the box and

sphere an equal distance away from the cylinder

• Name the sphere ‘Hospital’ and the box ‘Lava’

• Attach a Rigidbody to the cylinder, disable ‘Use Gravity’,

enable ‘Is Kinematic’, turn collision to Continuous

• Create a new script called NPC and attach it to the

cylinder

More Unity 101 – Triggers and Collisions

Page 16: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Triggers and Collisions

• Create an UpdatePosition() method to cause the cylinder

to move around in a circle

• Add a public float called health to the NPC class

Page 17: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Triggers and Collisions

• Create two new scripts called Lava and Hospital and

attach each to their respective objects

• Check to see if the object that has triggered it has an

NPC component and if it does, modify the health

Page 18: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Triggers and Collisions

• The almighty Collision / Trigger chart…

Page 19: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Destroy & OnDestroy

• When we reach 0 health, we are going to destroy this object

• Unity will inform our object of the behavior’s destruction

through the OnDestroy() method

• We will create a new Game Object, attach a TextMesh

component to it and set the text to say “Game Over”

Page 20: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Destroy & OnDestroy

• When you destroy the object and the text appears, you will

notice that it is backwards, this is because of the text facing

the negative Z axis instead of the positive

• To flip it around we need to use the LookAt command with a

vector in the opposite direction

Page 21: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Destroy & OnDestroy

Camera

Look atDirection

Text

Flipped Lookat Direction

− 𝐶𝑎𝑚𝑒𝑟𝑎 − 𝑇𝑒𝑥𝑡 + 𝑇𝑒𝑥𝑡 = 𝐹𝑙𝑖𝑝𝑝𝑒𝑑

2 ∗ 𝑇𝑒𝑥𝑡 − 𝐶𝑎𝑚𝑒𝑟𝑎 = 𝐹𝑙𝑖𝑝𝑝𝑒𝑑

Page 22: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Pulling it All Together

• Let’s make a ball-spewing emitter!

• Create a new scene, place an empty game object and a

sphere in the scene

• Drag and drop the sphere into your project folder to create a

prefab

• Create two new scripts called Spit and SphereSpitter

Page 23: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Sphere

Page 24: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Sphere Spitter

Page 25: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Pulling it All Together

• Attach Sphere script to your

sphere object and press the

‘Apply’ button in the inspector

• Delete the sphere from your

scene

• Attach SphereSpitter script to

your empty Game Object

• Drag the Prefab sphere from

your project folder into

the SphereSpitter script

Page 26: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Pulling it All Together

Page 27: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Collisions + Sound

• On your sphere prefab, add

an AudioSource component

• Download a sound effect that

reminds you of something

being hit

• Place the sound effect in your

project and set it as the

AudioSource’s clip

• Turn off play on awake and

set spatial blending to 3D

• Open the Sphere script

Page 28: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Collisions + Sound

Page 29: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Collisions + Sound

Page 30: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Physic Materials

• Objects that are controlled by

the Physics system can have a

Physic material that defines

properties such as bounciness

• Place a box below the

explosion of spheres and

stretch it out in the X& Z axes

• Create a Physic material and

attach it to the ground place

in the collider

Page 31: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Physic Materials

• The Physic material holds

properties for friction (stationary

and moving) and how much

energy is absorbed during a

collision

• Also includes how energy should

be handled when colliding with

other objects

• Try playing with the values to see

how they impact the sphere’s

striking the surface of the plane

Page 32: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Rays & Tags

• Ray casting allows us to use a vector to detect

intersections

• We provide the starting point, a direction and a

maximum distance

• Will return a bool value of either true or false,

indicating if we had a successful hit

x,y,z

Direction& length

Page 33: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Rays & Tags

Page 34: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Rays & Tags

• Create a new script called Raycaster

• Create Color variables for the stored color and hover color

• In the update function we create a RaycastHit to get information if

we successfully hit an object

• Check to see if the object has a tag of ‘hoverable’

• Check to see if hoverObject is null and if it is, store the object and

color and change its appearance

• If it is not null and the hover object is something different then make

the old hover object the stored color

• If no intersection is performed and the hoverObject is not null then

set the hoverObject back to the default color and set hoverObject to

null

Page 35: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Rays & Tags

• Create the ‘hoverable’ tag

by clicking on a

gameobject and selecting

the tag drop down and

pressing add tag

• Within the Tags and Layers

window, press the plus

button and name your

new tag

• When using tags you are

using strings and they

must match perfectly,

including case

Page 36: Unity Physics - users.rowan.eduusers.rowan.edu/~lecakes/Materials/VR_Class_03a_UnityPhysics.pdf · • To randomly fill an array with integer values we need to use a different method

More Unity 101 – Rays & Tags