Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice...

35
Game Programming

Transcript of Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice...

Page 1: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Game Programming

Page 2: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Unity3dUnity3d

High quality game engine.

Very nice game editor

Terrain editor

Scripting in C#, JavaScript, Boo (dialect of Python)

Intuitive scene graph

Intuitive disk file system

Great documentation

Page 3: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Unity3dUnity3d

Page 4: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Unity Demos/TutorialsUnity Demos/TutorialsAngry Botscripting tutorials and others

Beginning Scripting and Intermediate Scripting: a good overview of C# and special Unity functions

Should watch all.Runtime Classes

Select UnityEngine/Classes

Page 6: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Debugging

Debugging Tools

function Start () {

Debug.Log("Start called on GameObject "+gameObject.name);

}

function Update () {

Debug.Log("Update called at time "+Time.time);

}

Page 7: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Debugging

using UnityEngine;using System.Collections;

public class spin : MonoBehaviour { public float speed =10f; int count;

void Start(){ count=0; }

// Update is called once per frame

void Update () { transform.Rotate (Vector3.up, speed * Time.deltaTime); if (count%1000 == 0){ Debug.Log("Update called at time" +Time.time); } count++; }

}

Page 9: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Prefabs

It is often useful to have a number of duplicate items that edited and changed as a group. To do this one creates a Prefab object. Then all copies of that object can be changed by editing one of the group.

Asset -> Create Prefab

Drag object into prefab asset pane.

Page 10: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Animation

From asset store get Skeleton Pack

Drag weaponless skeleton to Hierarchy.

Assets →Import Package → Scripts → import

In Hierarchy →Standard Assets → Scripts → Camera Scripts → Mouse Orbit to Main Camera.

Page 11: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Animation

Drag Skeleton to Mouse Orbit Target

Press Play.

Set Mouse Orbit Distance, etc to taste.

Expand skeleton in HierarchyExpand Bio01

Click on skeleton

Page 12: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Animation

In Hierarchy select skeleton → skeleton and look at inspector.

A SkinnedMeshRenderer Component. SkinnedMeshRenderer, like MeshRendererer, is a subclass of Renderer. However, the SkinnedMeshRenderer Component renders the Mesh that wraps around the skeleton and follows its joints as they move. The Mesh essentially forms the skin around a skeleton, so the process of mapping such a Mesh to a skeleton is often called skinning.

In Hierarchy select skeleton

Change animation

Press play

Page 13: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Animation

Go to Assets → Skeleton Data → skeleton and look at inspector.

Select Animation

Select run in clips

Choose Wrap Mode → loop

run

Page 14: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Animation

Select Edit → Project Settings → Quality.

Quality settings features can be edited. Click on ? at top right of Inspector for detail.

Page 15: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Sound

Get Free music pack from asset store.

Select Monster Screens and drag from Assets → Free Pack → Monster screams to Hierarchy

Set Parameters in inspector

Play

Page 16: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Physics

Add an alley 5x1x5

Get 18 FREE Substances from Asset Store

Add wood and wood normals, bumped specular, increase size to 10x1x50 and tiling to 5,5,5,5

Add physic materials

Add sphere

Rigid bodies

Page 17: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Physics

Physics constants

Add materials and rigid body to sphere.

FixedUpdate

Look at TimeManager

Page 18: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Physics

Add script rollvar mousepowerx:float = 1.0;

var mousepowery:float = 1.0;

var maxvelocity:float=400.0;

private var forcey:float=0.0;

private var forcex:float=0.0;

private var isRolling:boolean=false;

private var floorTag:String = "alley";

var maxVelocitySquared:float=400.0;

Page 19: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Physics

Add script roll (continued)function Update() {

forcex = mousepowerx*Input.GetAxis("Mouse X")/Time.deltaTime;

forcey = mousepowery*Input.GetAxis("Mouse Y")/Time.deltaTime;

}

function FixedUpdate() {

if (isRolling && rigidbody.velocity.sqrMagnitude<maxVelocitySquared) {

rigidbody.AddForce(forcex,0,forcey);

} }

function OnCollisionEnter(collider:Collision) {

if (collider.gameObject.tag == floorTag) {

isRolling = true;

} }

Page 20: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Physics

Add script roll (continued)function OnCollisionStay(collider:Collision) {

if (collider.gameObject.tag == floorTag) {

isRolling = true;

} }

function OnCollisionExit(collider:Collision) {

if (collider.gameObject.tag == floorTag) {

isRolling = false;

} }

Page 21: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Physics

Add smooth follow to camera.

Page 22: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Assignment

Complete the tutorials at

http://unity3d.com/learn/tutorials/modules/beginner/physics

Page 24: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Physics

Page 28: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

28

Unity GUI System

Many game GUI's are graphic objects with a callback function attached to mouse/keyboard events realized on the object. The graphics and callbacks are setup during initialization. The code to initialize an object is general complex. As the callbacks are static, they become quite complex if the object is used frequently in different contexts.

The is called retained mode GUI

Page 30: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Game Programming

Page 31: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Misc

Setting Action Keys

Edit → Project Settings → Input → Axes

Editing Curves

Export/Import Assets.

DirectX settings

Edit → Project → Player Settings → Other

Math.lerp

IK (inverse kinematics)

Body Mask

Page 32: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Animation Rigging

Import Animation

Select Skeleton Data in Assets.

Select skeleton.

Select Rig.

Select Humanoid/Create from Model.

Configure

Mapping → automap

Muscles

Done

Page 33: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Animation Controller

An example of visual programming like OpenDX

Sets up finite state machine states

Defines transitions as unidirectional links

Actions defined in controller script

Page 34: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Animation Controller

Mecanim Tutorial starting at 17.03

Add animation 69_15 and 69_16 to Huge Mocap Library demo controler

Blend tree can be used to switch between animations

Page 35: Game Programmingruttan/GameProg/lectures/unity0.pdf · Unity3d High quality game engine. Very nice game editor ... Unity3d. Unity Demos/Tutorials Angry Bot scripting tutorials and

Assignment 3

Reimplement Assignment 2, but use an animation to turn the skeleton in place. The run and turn animations should be blended together. Several different animations may be required depending on the direction the avator is facing.