SlideShare una empresa de Scribd logo
1 de 46
Descargar para leer sin conexión
INTERACTIVE OBJECTS IN
GAMING APPLICATIONS
Basic principles and practical scenarios in Unity
June 2013Stavros Vassos Sapienza University of Rome, DIAG, Italy vassos@dis.uniroma1.it
Interactive objects in games
2
 Pathfinding
 Part 1: A* heuristic search on a grid
 Part 2: Examples in Unity
 Part 3: Beyond the basics
 Action-based decision making
 AI Architectures
Pathfinding: Examples in Unity
3
Pathfinding: Examples in Unity
4
 We will use a sample project in Pathfinding.zip
 Based on
 Aron Granberg A* Pathfinding project (free version)
 http://arongranberg.com/astar/download
 http://arongranberg.com/astar/docs/getstarted.php
 3D Platformer Tutorial by Unity (free)
 http://u3d.as/content/unity-technologies/3d-platformer-
tutorial/
 Unzip, open project, and load scene “Pathfinding on
a plane” (it is already ready for experimentation)
Pathfinding: Examples in Unity
5
 We will use a sample project in Pathfinding.zip
 Based on
 Aron Granberg A* Pathfinding project (free version)
 http://arongranberg.com/astar/download
 http://arongranberg.com/astar/docs/getstarted.php
 3D Platformer Tutorial by Unity (free)
 http://u3d.as/content/unity-technologies/3d-platformer-
tutorial/
 Unzip, open project, and load scene “Pathfinding on
a plane” (it is already ready for experimentation)
Many other packages available, paid, free, or
freemium. You can also write your own!
Pathfinding: Examples in Unity
6
 Creating the scene
 Simple plane
 Rigid body (docs.unity3d.com:Rigidbody)
 Box collider (docs.unity3d.com:BoxCollider)
 Checkerboard material
 Crates
 Rigid body
 Box collider
 Some material that looks nice
Pathfinding: Examples in Unity
7
 Creating the scene
 Simple plane
 Rigid body (docs.unity3d.com:Rigidbody)
 Box collider (docs.unity3d.com:BoxCollider)
 Checkerboard material
 We want to note that this is the “ground” for navigation
 Crates
 Rigid body
 Box collider
 Some material that looks nice
 We want to note that these are “obstacles” for navigation
Pathfinding: Examples in Unity
8
 Tags: mostly for finding game objects
(docs.unity3d.com:Tags)
 Layers: mostly for rendering purposes
(docs.unity3d.com:Layers)
Pathfinding: Examples in Unity
9
 Here we specify two layers
 Ground
 Obstacles
 And set the plane and crates to
the correct one
Pathfinding: Examples in Unity
10
 Creating the scene
 Adding a character that we will instruct to move around
 Lerpz prefab from 3D Platformer Tutorial of Unity
 Character controller component
Pathfinding: Examples in Unity
11
 Essentially a capsule-shaped Collider
(docs.unity3d.com:CharacterController)
 Can be told to move in some direction from a script
 Can carry out the movement but be constrained by
collisions (also step offset, slope limit)
Pathfinding: Examples in Unity
12
 Aron’s pathfinding package
 Adding pathfinding functionality in the scene
 Create empty object
 Name it “A* Pathfinder”
 Add the AstarPath script from Aron’s package
 This is a unique central hub for computing paths
 Adding pathfiding functionality to the character
 Add the Seeker component
Pathfinding: Ex. in Unity
13
 Specify the parameters of the
AstarPath script
 Grid Graph
 Position/size
 4-connected/8-connected
 Cut corners
 Collision testing
 Type/size of collider
 Mask: Use Obstacle Layer
 Scan and observe!
Pathfinding: Ex. in Unity
14
 Capsule collider with diameter 2
 8-connected
Pathfinding: Ex. in Unity
15
 Capsule collider with diameter 2
 8-connected
“Cut corners”
option affects
these edges
Pathfinding: Ex. in Unity
16
 Capsule collider with diameter 6
 4-connected
Pathfinding: Examples in Unity
17
 Using the Seeker script to get paths for Lerpz
 Astar AI1
 Astar AI2
 Astar AI3
 Astar AI4
 Based on sample code in Aron’s pathfinding package
Pathfinding: Examples in Unity
18
 Using the Seeker script to get paths for Lerpz
public class AstarAI1 : MonoBehaviour {
public Vector3 targetPosition;
public void Start () {
//Get a reference to the Seeker component
Seeker seeker = GetComponent<Seeker>();
//Start a new path to the targetPosition,
//return the result to the OnPathComplete function
seeker.StartPath (transform.position,targetPosition,
OnPathComplete);
}
public void OnPathComplete (Path p) {
Debug.Log ("Yey, we got a path back!");
}
}
Pathfinding: Examples in Unity
19
 Astar A1 script calls the Seeker's StartPath function
 The Seeker creates a new Path instance and sends it to
the AstarPath script (in the A* Pathfinder game-object)
 AstarPath script puts the path in a queue, when
available, it processes the path by searching the grid in
the A* heuristic search manner we described earlier
 AstarPAth script calls the function OnPathComplete with
the path it found as a parameter p
 p.VectorPath holds a list of Vector3 objects that show
how the destination can be reached
Pathfinding: Examples in Unity
20
 Seeker option to display path via gizmos
Pathfinding: Examples in Unity
21
 Post-processing path via modifiers (here: Funnell mod.)
Pathfinding: Examples in Unity
22
 Using Astar AI1 script, Lerpz is able to find out how
to get to the target object (fuel cell prefab)
 So, let’s make Lerpz move!
 Accessing other components
(docs.unity3d.com:Accessing)
 SimpleMove function of Character component
(docs.unity3d.com:CharacterController.SimpleMove)
 FixedUpdate instead Update for physics-related things
(docs.unity3d.com:FixedUpdate,
unity3d.com/tutorials:Update-and-Fixedupdate)
Pathfinding: Examples in Unity
23
public class AstarAI2 : MonoBehaviour {
...
//The calculated path
public Path path;
//The character’s speed
public float speed = 100;
//The distance to a waypoint for moving to the next
public float nextWaypointDistance = 3;
//The waypoint we are currently moving towards
private int currentWaypoint = 0;
public void Start () {
... }
public void OnPathComplete (Path p) {
... }
public void FixedUpdate () {
... }
}
Pathfinding: Examples in Unity
24
public class AstarAI2 : MonoBehaviour {
public void FixedUpdate () {
//Check if we reached the destination
if (currentWaypoint >= path.vectorPath.Count) {
Debug.Log ("End Of Path Reached");
return;
}
//Direction to the next waypoint
Vector3 dir = (path.vectorPath[currentWaypoint]-
transform.position).normalized;
dir *= speed * Time.fixedDeltaTime;
controller.SimpleMove (dir);
//Check if we need to switch to the next waypoint
if (Vector3.Distance(transform.position,
path.vectorPath[currentWaypoint]) < nextWaypointDistance) {
currentWaypoint++;
return;
}
}
Pathfinding: Examples in Unity
25
 Lerpz moves but without animating its parts
 Basic animation of pre-defined clips
 Animation component (docs.unity3d.com:Animation)
 Play(“walk”), Stop()
Pathfinding: Examples in Unity
26
 Lerpz.fbx
 Embedded animations that have
been created outside of Unity for
the 3D model
 Available for
 Playing
 Looping
 Blending
Pathfinding: Examples in Unity
27
public class AstarAI3 : MonoBehaviour {
...
//Cashed reference to Animation component
public Animation anim;
public void Start () { ...
anim = transform.GetComponentInChildren<Animation>();
}
public void OnPathComplete (Path p) { ...
anim.Play("walk");
}
public void FixedUpdate () { ...
if (currentWaypoint >= path.vectorPath.Count) {
Debug.Log ("End Of Path Reached");
anim.Stop();
return;
...
}
}
}
Pathfinding: Examples in Unity
28
 We can do better than that!
 Turn when changing direction while moving
 Slow down before reaching destination
 Nicer/smoother paths
 Plan/replan for a moving target
 Plan/replan with a dynamic grid
Pathfinding: Examples in Unity
29
 We can do better than that!
 Astar AI4 script
 Target is a transform of an object in the game-world
 Repath rate
 Turning speed
 Slowdown distance
 Fancy code with coroutines and yield
(docs.unity3d.com:Coroutines-Yield)
Pathfinding: Examples in Unity
30
 We can do better than that!
 TargetMover script attached on main camera
 Moves target to position of mouse
 Always on or only when double click is detected
 Use the fuel cell as a target
 ObjectPlacer script attached on main camera
 Pressing “p” adds a game-object instance at ray collision
with a game-object in the scene
 Use it to place crates
 Pressing “r” removes game-object
Pathfinding: Examples in Unity
31
public class TargetMover : MonoBehaviour {
...
//Handle continuous move in Update() function
void Update () {
if (!onlyOnDoubleClick && cam != null) {
UpdateTargetPosition ();
}
}
public void UpdateTargetPosition () {
//Define a ray from eye to mouse position
Ray ray = cam.ScreenPointToRay (Input.mousePosition);
//Do raycast and if it hits the given mask layer
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, mask)){
//transport the target at the hit position
target.position = hit.point;
}
}
Pathfinding: Examples in Unity
32
public class ObjectPlacer : MonoBehaviour {
...
void Update () {
if (Input.GetKeyDown ("p")) {
PlaceObject ();
}
if (Input.GetKeyDown ("r")) {
RemoveObject ();
}
public void PlaceObject () {
//Cast a ray, etc
...
}
public void RemoveObject () {
//Cast a ray, etc
...
}
}
Pathfinding: Examples in Unity
33
 Scripts
 Astar AI1
 Astar AI2
 Astar AI3
 Astar AI4
 TargetMover
 ObjectPlacer
 Either as originally from Aron’s Pathfinding Project or
with slight modifications
 Use the ones in the project in Pathfinding.zip
Pathfinding: Examples in Unity
34
 Many crates
Pathfinding: Examples in Unity
35
 Many crates – How does Lerpz know to avoid them?
Pathfinding: Examples in Unity
36
public void PlaceObject () {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
//If we hit an object
if ( Physics.Raycast (ray, out hit, Mathf.Infinity)) {
Vector3 p = hit.point;
//Instantiate a new game-object
GameObject obj = (GameObject)GameObject.Instantiate
(go,p,Quaternion.identity);
//Update the pathfinding graph
Bounds b = obj.collider.bounds;
GraphUpdateObject guo = new GraphUpdateObject(b);
AstarPath.active.UpdateGraphs (guo);
}
}
Pathfinding: Examples in Unity
37
 Local updates are faster if we have a huge map
 But can be problematic when objects end up farther
away or may fall on the ground later than the update
Pathfinding: Examples in Unity
38
 Many Lerpzs!
Pathfinding: Examples in Unity
39
 Many Lerpzs! FPS decreases even for just 10 NPCs
Pathfinding: Examples in Unity
40
 Grids can only go so far
Pathfinding: Examples in Unity
41
 Open scene “Pathfiding on platforms”
Pathfinding: Unity
42
 Height testing
 Similar to obstacles
 Use “Ground” layer to identify
areas that are walkable
 Raycasting
Pathfinding: Examples in Unity
43
 We can still tweak our grid to do the job
Pathfinding: Unity
44
 Or we can use a graph (Pointgraph, dist=4)
Pathfinding: Unity
45
 Or we can use a graph (Pointgraph, dist=4)
Pathfinding Part 3: Beyond the basics
46
 Many neat tricks for efficient/nice pathfinding
 Alternative game-world representations
 Beyond A*
 Better heuristics

Más contenido relacionado

Destacado

Unity introduction for programmers
Unity introduction for programmersUnity introduction for programmers
Unity introduction for programmersNoam Gat
 
Introduction to Unity3D and Building your First Game
Introduction to Unity3D and Building your First GameIntroduction to Unity3D and Building your First Game
Introduction to Unity3D and Building your First GameSarah Sexton
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsLinkedIn
 

Destacado (6)

Unity introduction for programmers
Unity introduction for programmersUnity introduction for programmers
Unity introduction for programmers
 
Unity3D Programming
Unity3D ProgrammingUnity3D Programming
Unity3D Programming
 
Unity 3d Basics
Unity 3d BasicsUnity 3d Basics
Unity 3d Basics
 
Unity presentation
Unity presentationUnity presentation
Unity presentation
 
Introduction to Unity3D and Building your First Game
Introduction to Unity3D and Building your First GameIntroduction to Unity3D and Building your First Game
Introduction to Unity3D and Building your First Game
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving Cars
 

Similar a Pathfinding - Part 2: Examples in Unity

【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップUnity Technologies Japan K.K.
 
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップUnite2017Tokyo
 
java question Fill the add statement areaProject is to wo.pdf
java question Fill the add statement areaProject is to wo.pdfjava question Fill the add statement areaProject is to wo.pdf
java question Fill the add statement areaProject is to wo.pdfdbrienmhompsonkath75
 
Session 9 Android Web Services - Part 2.pdf
Session 9 Android Web Services - Part 2.pdfSession 9 Android Web Services - Part 2.pdf
Session 9 Android Web Services - Part 2.pdfEngmohammedAlzared
 
6.1.1一步一步学repast代码解释
6.1.1一步一步学repast代码解释6.1.1一步一步学repast代码解释
6.1.1一步一步学repast代码解释zhang shuren
 
Creating an Uber Clone - Part XIX - Transcript.pdf
Creating an Uber Clone - Part XIX - Transcript.pdfCreating an Uber Clone - Part XIX - Transcript.pdf
Creating an Uber Clone - Part XIX - Transcript.pdfShaiAlmog1
 
Java design patterns
Java design patternsJava design patterns
Java design patternsShawn Brito
 
School For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine BasicsSchool For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine BasicsNick Pruehs
 
The 2016 Android Developer Toolbox [MOBILIZATION]
The 2016 Android Developer Toolbox [MOBILIZATION]The 2016 Android Developer Toolbox [MOBILIZATION]
The 2016 Android Developer Toolbox [MOBILIZATION]Nilhcem
 
05 pig user defined functions (udfs)
05 pig user defined functions (udfs)05 pig user defined functions (udfs)
05 pig user defined functions (udfs)Subhas Kumar Ghosh
 
Reactive programming with tracker
Reactive programming with trackerReactive programming with tracker
Reactive programming with trackerDesignveloper
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptxGuy Komari
 
Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)Korhan Bircan
 
Actionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core ConceptActionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core ConceptOUM SAOKOSAL
 
Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced Flink Forward
 
Augmented Reality With FlarToolkit and Papervision3D
Augmented Reality With FlarToolkit and Papervision3DAugmented Reality With FlarToolkit and Papervision3D
Augmented Reality With FlarToolkit and Papervision3DRoman Protsyk
 

Similar a Pathfinding - Part 2: Examples in Unity (20)

【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
 
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
【Unite 2017 Tokyo】もっと気軽に、動的なコンテンツ配信を ~アセットバンドルの未来と開発ロードマップ
 
java question Fill the add statement areaProject is to wo.pdf
java question Fill the add statement areaProject is to wo.pdfjava question Fill the add statement areaProject is to wo.pdf
java question Fill the add statement areaProject is to wo.pdf
 
Unity3 d devfest-2014
Unity3 d devfest-2014Unity3 d devfest-2014
Unity3 d devfest-2014
 
Games 3 dl4-example
Games 3 dl4-exampleGames 3 dl4-example
Games 3 dl4-example
 
Android workshop
Android workshopAndroid workshop
Android workshop
 
Session 9 Android Web Services - Part 2.pdf
Session 9 Android Web Services - Part 2.pdfSession 9 Android Web Services - Part 2.pdf
Session 9 Android Web Services - Part 2.pdf
 
6.1.1一步一步学repast代码解释
6.1.1一步一步学repast代码解释6.1.1一步一步学repast代码解释
6.1.1一步一步学repast代码解释
 
Creating an Uber Clone - Part XIX - Transcript.pdf
Creating an Uber Clone - Part XIX - Transcript.pdfCreating an Uber Clone - Part XIX - Transcript.pdf
Creating an Uber Clone - Part XIX - Transcript.pdf
 
Java design patterns
Java design patternsJava design patterns
Java design patterns
 
School For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine BasicsSchool For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine Basics
 
The 2016 Android Developer Toolbox [MOBILIZATION]
The 2016 Android Developer Toolbox [MOBILIZATION]The 2016 Android Developer Toolbox [MOBILIZATION]
The 2016 Android Developer Toolbox [MOBILIZATION]
 
05 pig user defined functions (udfs)
05 pig user defined functions (udfs)05 pig user defined functions (udfs)
05 pig user defined functions (udfs)
 
Reactive programming with tracker
Reactive programming with trackerReactive programming with tracker
Reactive programming with tracker
 
SAADATMAND_PYTHON
SAADATMAND_PYTHONSAADATMAND_PYTHON
SAADATMAND_PYTHON
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptx
 
Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)
 
Actionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core ConceptActionscript 3 - Session 4 Core Concept
Actionscript 3 - Session 4 Core Concept
 
Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced Apache Flink Training: DataStream API Part 2 Advanced
Apache Flink Training: DataStream API Part 2 Advanced
 
Augmented Reality With FlarToolkit and Papervision3D
Augmented Reality With FlarToolkit and Papervision3DAugmented Reality With FlarToolkit and Papervision3D
Augmented Reality With FlarToolkit and Papervision3D
 

Más de Stavros Vassos

Pathfinding - Part 3: Beyond the basics
Pathfinding - Part 3: Beyond the basicsPathfinding - Part 3: Beyond the basics
Pathfinding - Part 3: Beyond the basicsStavros Vassos
 
Pathfinding - Part 1: Α* heuristic search
Pathfinding - Part 1: Α* heuristic searchPathfinding - Part 1: Α* heuristic search
Pathfinding - Part 1: Α* heuristic searchStavros Vassos
 
Intro to AI STRIPS Planning & Applications in Video-games Lecture1-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture1-Part1Intro to AI STRIPS Planning & Applications in Video-games Lecture1-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture1-Part1Stavros Vassos
 
Intro to AI STRIPS Planning & Applications in Video-games Lecture6-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture6-Part1Intro to AI STRIPS Planning & Applications in Video-games Lecture6-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture6-Part1Stavros Vassos
 
Intro to AI STRIPS Planning & Applications in Video-games Lecture5-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture5-Part1Intro to AI STRIPS Planning & Applications in Video-games Lecture5-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture5-Part1Stavros Vassos
 
Intro to AI STRIPS Planning & Applications in Video-games Lecture4-Part2
Intro to AI STRIPS Planning & Applications in Video-games Lecture4-Part2Intro to AI STRIPS Planning & Applications in Video-games Lecture4-Part2
Intro to AI STRIPS Planning & Applications in Video-games Lecture4-Part2Stavros Vassos
 
Intro to AI STRIPS Planning & Applications in Video-games Lecture4-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture4-Part1Intro to AI STRIPS Planning & Applications in Video-games Lecture4-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture4-Part1Stavros Vassos
 
Intro to AI STRIPS Planning & Applications in Video-games Lecture3-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture3-Part1Intro to AI STRIPS Planning & Applications in Video-games Lecture3-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture3-Part1Stavros Vassos
 
Intro to AI STRIPS Planning & Applications in Video-games Lecture2-Part2
Intro to AI STRIPS Planning & Applications in Video-games Lecture2-Part2Intro to AI STRIPS Planning & Applications in Video-games Lecture2-Part2
Intro to AI STRIPS Planning & Applications in Video-games Lecture2-Part2Stavros Vassos
 
Intro to AI STRIPS Planning & Applications in Video-games Lecture1-Part2
Intro to AI STRIPS Planning & Applications in Video-games Lecture1-Part2Intro to AI STRIPS Planning & Applications in Video-games Lecture1-Part2
Intro to AI STRIPS Planning & Applications in Video-games Lecture1-Part2Stavros Vassos
 
Intro to AI STRIPS Planning & Applications in Video-games Lecture6-Part2
Intro to AI STRIPS Planning & Applications in Video-games Lecture6-Part2Intro to AI STRIPS Planning & Applications in Video-games Lecture6-Part2
Intro to AI STRIPS Planning & Applications in Video-games Lecture6-Part2Stavros Vassos
 
The SimpleFPS Planning Domain: A PDDL Benchmark for Proactive NPCs
The SimpleFPS Planning Domain: A PDDL Benchmark for Proactive NPCsThe SimpleFPS Planning Domain: A PDDL Benchmark for Proactive NPCs
The SimpleFPS Planning Domain: A PDDL Benchmark for Proactive NPCsStavros Vassos
 

Más de Stavros Vassos (12)

Pathfinding - Part 3: Beyond the basics
Pathfinding - Part 3: Beyond the basicsPathfinding - Part 3: Beyond the basics
Pathfinding - Part 3: Beyond the basics
 
Pathfinding - Part 1: Α* heuristic search
Pathfinding - Part 1: Α* heuristic searchPathfinding - Part 1: Α* heuristic search
Pathfinding - Part 1: Α* heuristic search
 
Intro to AI STRIPS Planning & Applications in Video-games Lecture1-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture1-Part1Intro to AI STRIPS Planning & Applications in Video-games Lecture1-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture1-Part1
 
Intro to AI STRIPS Planning & Applications in Video-games Lecture6-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture6-Part1Intro to AI STRIPS Planning & Applications in Video-games Lecture6-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture6-Part1
 
Intro to AI STRIPS Planning & Applications in Video-games Lecture5-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture5-Part1Intro to AI STRIPS Planning & Applications in Video-games Lecture5-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture5-Part1
 
Intro to AI STRIPS Planning & Applications in Video-games Lecture4-Part2
Intro to AI STRIPS Planning & Applications in Video-games Lecture4-Part2Intro to AI STRIPS Planning & Applications in Video-games Lecture4-Part2
Intro to AI STRIPS Planning & Applications in Video-games Lecture4-Part2
 
Intro to AI STRIPS Planning & Applications in Video-games Lecture4-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture4-Part1Intro to AI STRIPS Planning & Applications in Video-games Lecture4-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture4-Part1
 
Intro to AI STRIPS Planning & Applications in Video-games Lecture3-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture3-Part1Intro to AI STRIPS Planning & Applications in Video-games Lecture3-Part1
Intro to AI STRIPS Planning & Applications in Video-games Lecture3-Part1
 
Intro to AI STRIPS Planning & Applications in Video-games Lecture2-Part2
Intro to AI STRIPS Planning & Applications in Video-games Lecture2-Part2Intro to AI STRIPS Planning & Applications in Video-games Lecture2-Part2
Intro to AI STRIPS Planning & Applications in Video-games Lecture2-Part2
 
Intro to AI STRIPS Planning & Applications in Video-games Lecture1-Part2
Intro to AI STRIPS Planning & Applications in Video-games Lecture1-Part2Intro to AI STRIPS Planning & Applications in Video-games Lecture1-Part2
Intro to AI STRIPS Planning & Applications in Video-games Lecture1-Part2
 
Intro to AI STRIPS Planning & Applications in Video-games Lecture6-Part2
Intro to AI STRIPS Planning & Applications in Video-games Lecture6-Part2Intro to AI STRIPS Planning & Applications in Video-games Lecture6-Part2
Intro to AI STRIPS Planning & Applications in Video-games Lecture6-Part2
 
The SimpleFPS Planning Domain: A PDDL Benchmark for Proactive NPCs
The SimpleFPS Planning Domain: A PDDL Benchmark for Proactive NPCsThe SimpleFPS Planning Domain: A PDDL Benchmark for Proactive NPCs
The SimpleFPS Planning Domain: A PDDL Benchmark for Proactive NPCs
 

Último

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Último (20)

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 

Pathfinding - Part 2: Examples in Unity

  • 1. INTERACTIVE OBJECTS IN GAMING APPLICATIONS Basic principles and practical scenarios in Unity June 2013Stavros Vassos Sapienza University of Rome, DIAG, Italy vassos@dis.uniroma1.it
  • 2. Interactive objects in games 2  Pathfinding  Part 1: A* heuristic search on a grid  Part 2: Examples in Unity  Part 3: Beyond the basics  Action-based decision making  AI Architectures
  • 4. Pathfinding: Examples in Unity 4  We will use a sample project in Pathfinding.zip  Based on  Aron Granberg A* Pathfinding project (free version)  http://arongranberg.com/astar/download  http://arongranberg.com/astar/docs/getstarted.php  3D Platformer Tutorial by Unity (free)  http://u3d.as/content/unity-technologies/3d-platformer- tutorial/  Unzip, open project, and load scene “Pathfinding on a plane” (it is already ready for experimentation)
  • 5. Pathfinding: Examples in Unity 5  We will use a sample project in Pathfinding.zip  Based on  Aron Granberg A* Pathfinding project (free version)  http://arongranberg.com/astar/download  http://arongranberg.com/astar/docs/getstarted.php  3D Platformer Tutorial by Unity (free)  http://u3d.as/content/unity-technologies/3d-platformer- tutorial/  Unzip, open project, and load scene “Pathfinding on a plane” (it is already ready for experimentation) Many other packages available, paid, free, or freemium. You can also write your own!
  • 6. Pathfinding: Examples in Unity 6  Creating the scene  Simple plane  Rigid body (docs.unity3d.com:Rigidbody)  Box collider (docs.unity3d.com:BoxCollider)  Checkerboard material  Crates  Rigid body  Box collider  Some material that looks nice
  • 7. Pathfinding: Examples in Unity 7  Creating the scene  Simple plane  Rigid body (docs.unity3d.com:Rigidbody)  Box collider (docs.unity3d.com:BoxCollider)  Checkerboard material  We want to note that this is the “ground” for navigation  Crates  Rigid body  Box collider  Some material that looks nice  We want to note that these are “obstacles” for navigation
  • 8. Pathfinding: Examples in Unity 8  Tags: mostly for finding game objects (docs.unity3d.com:Tags)  Layers: mostly for rendering purposes (docs.unity3d.com:Layers)
  • 9. Pathfinding: Examples in Unity 9  Here we specify two layers  Ground  Obstacles  And set the plane and crates to the correct one
  • 10. Pathfinding: Examples in Unity 10  Creating the scene  Adding a character that we will instruct to move around  Lerpz prefab from 3D Platformer Tutorial of Unity  Character controller component
  • 11. Pathfinding: Examples in Unity 11  Essentially a capsule-shaped Collider (docs.unity3d.com:CharacterController)  Can be told to move in some direction from a script  Can carry out the movement but be constrained by collisions (also step offset, slope limit)
  • 12. Pathfinding: Examples in Unity 12  Aron’s pathfinding package  Adding pathfinding functionality in the scene  Create empty object  Name it “A* Pathfinder”  Add the AstarPath script from Aron’s package  This is a unique central hub for computing paths  Adding pathfiding functionality to the character  Add the Seeker component
  • 13. Pathfinding: Ex. in Unity 13  Specify the parameters of the AstarPath script  Grid Graph  Position/size  4-connected/8-connected  Cut corners  Collision testing  Type/size of collider  Mask: Use Obstacle Layer  Scan and observe!
  • 14. Pathfinding: Ex. in Unity 14  Capsule collider with diameter 2  8-connected
  • 15. Pathfinding: Ex. in Unity 15  Capsule collider with diameter 2  8-connected “Cut corners” option affects these edges
  • 16. Pathfinding: Ex. in Unity 16  Capsule collider with diameter 6  4-connected
  • 17. Pathfinding: Examples in Unity 17  Using the Seeker script to get paths for Lerpz  Astar AI1  Astar AI2  Astar AI3  Astar AI4  Based on sample code in Aron’s pathfinding package
  • 18. Pathfinding: Examples in Unity 18  Using the Seeker script to get paths for Lerpz public class AstarAI1 : MonoBehaviour { public Vector3 targetPosition; public void Start () { //Get a reference to the Seeker component Seeker seeker = GetComponent<Seeker>(); //Start a new path to the targetPosition, //return the result to the OnPathComplete function seeker.StartPath (transform.position,targetPosition, OnPathComplete); } public void OnPathComplete (Path p) { Debug.Log ("Yey, we got a path back!"); } }
  • 19. Pathfinding: Examples in Unity 19  Astar A1 script calls the Seeker's StartPath function  The Seeker creates a new Path instance and sends it to the AstarPath script (in the A* Pathfinder game-object)  AstarPath script puts the path in a queue, when available, it processes the path by searching the grid in the A* heuristic search manner we described earlier  AstarPAth script calls the function OnPathComplete with the path it found as a parameter p  p.VectorPath holds a list of Vector3 objects that show how the destination can be reached
  • 20. Pathfinding: Examples in Unity 20  Seeker option to display path via gizmos
  • 21. Pathfinding: Examples in Unity 21  Post-processing path via modifiers (here: Funnell mod.)
  • 22. Pathfinding: Examples in Unity 22  Using Astar AI1 script, Lerpz is able to find out how to get to the target object (fuel cell prefab)  So, let’s make Lerpz move!  Accessing other components (docs.unity3d.com:Accessing)  SimpleMove function of Character component (docs.unity3d.com:CharacterController.SimpleMove)  FixedUpdate instead Update for physics-related things (docs.unity3d.com:FixedUpdate, unity3d.com/tutorials:Update-and-Fixedupdate)
  • 23. Pathfinding: Examples in Unity 23 public class AstarAI2 : MonoBehaviour { ... //The calculated path public Path path; //The character’s speed public float speed = 100; //The distance to a waypoint for moving to the next public float nextWaypointDistance = 3; //The waypoint we are currently moving towards private int currentWaypoint = 0; public void Start () { ... } public void OnPathComplete (Path p) { ... } public void FixedUpdate () { ... } }
  • 24. Pathfinding: Examples in Unity 24 public class AstarAI2 : MonoBehaviour { public void FixedUpdate () { //Check if we reached the destination if (currentWaypoint >= path.vectorPath.Count) { Debug.Log ("End Of Path Reached"); return; } //Direction to the next waypoint Vector3 dir = (path.vectorPath[currentWaypoint]- transform.position).normalized; dir *= speed * Time.fixedDeltaTime; controller.SimpleMove (dir); //Check if we need to switch to the next waypoint if (Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]) < nextWaypointDistance) { currentWaypoint++; return; } }
  • 25. Pathfinding: Examples in Unity 25  Lerpz moves but without animating its parts  Basic animation of pre-defined clips  Animation component (docs.unity3d.com:Animation)  Play(“walk”), Stop()
  • 26. Pathfinding: Examples in Unity 26  Lerpz.fbx  Embedded animations that have been created outside of Unity for the 3D model  Available for  Playing  Looping  Blending
  • 27. Pathfinding: Examples in Unity 27 public class AstarAI3 : MonoBehaviour { ... //Cashed reference to Animation component public Animation anim; public void Start () { ... anim = transform.GetComponentInChildren<Animation>(); } public void OnPathComplete (Path p) { ... anim.Play("walk"); } public void FixedUpdate () { ... if (currentWaypoint >= path.vectorPath.Count) { Debug.Log ("End Of Path Reached"); anim.Stop(); return; ... } } }
  • 28. Pathfinding: Examples in Unity 28  We can do better than that!  Turn when changing direction while moving  Slow down before reaching destination  Nicer/smoother paths  Plan/replan for a moving target  Plan/replan with a dynamic grid
  • 29. Pathfinding: Examples in Unity 29  We can do better than that!  Astar AI4 script  Target is a transform of an object in the game-world  Repath rate  Turning speed  Slowdown distance  Fancy code with coroutines and yield (docs.unity3d.com:Coroutines-Yield)
  • 30. Pathfinding: Examples in Unity 30  We can do better than that!  TargetMover script attached on main camera  Moves target to position of mouse  Always on or only when double click is detected  Use the fuel cell as a target  ObjectPlacer script attached on main camera  Pressing “p” adds a game-object instance at ray collision with a game-object in the scene  Use it to place crates  Pressing “r” removes game-object
  • 31. Pathfinding: Examples in Unity 31 public class TargetMover : MonoBehaviour { ... //Handle continuous move in Update() function void Update () { if (!onlyOnDoubleClick && cam != null) { UpdateTargetPosition (); } } public void UpdateTargetPosition () { //Define a ray from eye to mouse position Ray ray = cam.ScreenPointToRay (Input.mousePosition); //Do raycast and if it hits the given mask layer RaycastHit hit; if (Physics.Raycast(ray, out hit, Mathf.Infinity, mask)){ //transport the target at the hit position target.position = hit.point; } }
  • 32. Pathfinding: Examples in Unity 32 public class ObjectPlacer : MonoBehaviour { ... void Update () { if (Input.GetKeyDown ("p")) { PlaceObject (); } if (Input.GetKeyDown ("r")) { RemoveObject (); } public void PlaceObject () { //Cast a ray, etc ... } public void RemoveObject () { //Cast a ray, etc ... } }
  • 33. Pathfinding: Examples in Unity 33  Scripts  Astar AI1  Astar AI2  Astar AI3  Astar AI4  TargetMover  ObjectPlacer  Either as originally from Aron’s Pathfinding Project or with slight modifications  Use the ones in the project in Pathfinding.zip
  • 34. Pathfinding: Examples in Unity 34  Many crates
  • 35. Pathfinding: Examples in Unity 35  Many crates – How does Lerpz know to avoid them?
  • 36. Pathfinding: Examples in Unity 36 public void PlaceObject () { Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); RaycastHit hit; //If we hit an object if ( Physics.Raycast (ray, out hit, Mathf.Infinity)) { Vector3 p = hit.point; //Instantiate a new game-object GameObject obj = (GameObject)GameObject.Instantiate (go,p,Quaternion.identity); //Update the pathfinding graph Bounds b = obj.collider.bounds; GraphUpdateObject guo = new GraphUpdateObject(b); AstarPath.active.UpdateGraphs (guo); } }
  • 37. Pathfinding: Examples in Unity 37  Local updates are faster if we have a huge map  But can be problematic when objects end up farther away or may fall on the ground later than the update
  • 38. Pathfinding: Examples in Unity 38  Many Lerpzs!
  • 39. Pathfinding: Examples in Unity 39  Many Lerpzs! FPS decreases even for just 10 NPCs
  • 40. Pathfinding: Examples in Unity 40  Grids can only go so far
  • 41. Pathfinding: Examples in Unity 41  Open scene “Pathfiding on platforms”
  • 42. Pathfinding: Unity 42  Height testing  Similar to obstacles  Use “Ground” layer to identify areas that are walkable  Raycasting
  • 43. Pathfinding: Examples in Unity 43  We can still tweak our grid to do the job
  • 44. Pathfinding: Unity 44  Or we can use a graph (Pointgraph, dist=4)
  • 45. Pathfinding: Unity 45  Or we can use a graph (Pointgraph, dist=4)
  • 46. Pathfinding Part 3: Beyond the basics 46  Many neat tricks for efficient/nice pathfinding  Alternative game-world representations  Beyond A*  Better heuristics