Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity 2018 Artificial Intelligence Cookbook

You're reading from   Unity 2018 Artificial Intelligence Cookbook Over 90 recipes to build and customize AI entities for your games with Unity

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher
ISBN-13 9781788626170
Length 334 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Jorge Palacios Jorge Palacios
Author Profile Icon Jorge Palacios
Jorge Palacios
Jorge Elieser P Garrido Jorge Elieser P Garrido
Author Profile Icon Jorge Elieser P Garrido
Jorge Elieser P Garrido
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Behaviors - Intelligent Movement 2. Navigation FREE CHAPTER 3. Decision Making 4. The New NavMesh API 5. Coordination and Tactics 6. Agent Awareness 7. Board Games and Applied Search AI 8. Learning Techniques 9. Procedural Content Generation 10. Miscellaneous 11. Other Books You May Enjoy

Targeting a projectile

Just as it's important to predict a projectile's landing point, it's also important to develop intelligent agents capable of aiming projectiles. It wouldn't be fun if our rugby-player agents weren't capable of passing the ball.

Getting ready

Just as in the previous recipe, we only need to expand the Projectile class.

How to do it...

Thanks to our previous hard work, this recipe is a real piece of cake:

  1. Create the GetFireDirection function:
public static Vector3 GetFireDirection (Vector3 startPos, Vector3 endPos, float speed) 
{ 
    // body 
}

  1. Solve the corresponding quadratic equation:
Vector3 direction = Vector3.zero; 
Vector3 delta = endPos - startPos; 
float a = Vector3.Dot(Physics.gravity, Physics.gravity); 
float b = -4 * (Vector3.Dot(Physics.gravity, delta) + speed * speed); 
float c = 4 * Vector3.Dot(delta, delta); 
if (4 * a * c > b * b) 
    return direction; 
float time0 = Mathf.Sqrt((-b + Mathf.Sqrt(b * b - 4 * a * c)) / (2*a)); 
float time1 = Mathf.Sqrt((-b - Mathf.Sqrt(b * b - 4 * a * c)) / (2*a)); 
  1. If shooting the projectile is feasible given the parameters, return a non-zero direction vector:
float time; 
if (time0 < 0.0f) 
{ 
    if (time1 < 0) 
        return direction; 
    time = time1; 
} 
else 
{ 
    if (time1 < 0) 
        time = time0; 
    else 
        time = Mathf.Min(time0, time1); 
} 
direction = 2 * delta - Physics.gravity * (time * time); 
direction = direction / (2 * speed * time); 
return direction; 

How it works...

Given a fixed speed, we solve the corresponding quadratic equation in order to obtain the desired direction (when at least a single one-time value is available), which doesn't need to be normalized because we already normalized the vector while setting up the projectile.

There's more...

Take account of the fact that we are returning a blank direction when the time is negative; it means that the speed is not sufficient. One way to overcome this is to define a function that tests different speeds and then shoot the projectile.

Another relevant improvement is to add an extra parameter of the type bool for those cases when we have two valid times (which means two possible arcs), and we need to shoot over an obstacle such as a wall:

if (isWall) 
    time = Mathf.Max(time0, time1); 
else 
    time = Mathf.Min(time0, time1); 
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime