Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
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 FREE CHAPTER 2. Navigation 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

Shooting a projectile

This is the stepping stone for scenarios where we want to have control over gravity-reliant objects, such as balls and grenades, so we can then predict the projectile's landing spot or be able to effectively shoot a projectile at a given target.

Getting ready

This recipe differs a little bit as it doesn't rely on the base AgentBehaviour class.

How to do it...

  1. Create the Projectile class, along with its member variables, to handle the physics:
using UnityEngine; 
using System.Collections; 
 
public class Projectile : MonoBehaviour 
{ 
    private bool set = false; 
    private Vector3 firePos; 
    private Vector3 direction; 
    private float speed; 
    private float timeElapsed; 
} 
  1. Define the Update function:
void Update () 
{ 
    if (!set) 
        return; 
    timeElapsed += Time.deltaTime; 
    transform.position = firePos + direction * speed * timeElapsed; 
    transform.position += Physics.gravity * (timeElapsed * timeElapsed) / 2.0f; 
    // extra validation for cleaning the scene 
    if (transform.position.y < -1.0f) 
        Destroy(this.gameObject);// or set = false; and hide it 
} 
  1. Finally, implement the Set function in order to fire the game object (for example, calling it after it is instantiated in the scene):
public void Set (Vector3 firePos, Vector3 direction, float speed) 
{ 
    this.firePos = firePos; 
    this.direction = direction.normalized; 
    this.speed = speed; 
    transform.position = firePos; 
    set = true; 
} 

How it works...

This behavior uses high school physics in order to generate the parabolic movement.

There's more...

We could also take another approach: implementing public properties in the script or declaring member variables as public, and instead of calling the Set function, having the script disabled by default in the prefab and enabling it after all the properties have been set. That way, we could easily apply the object pool pattern.

See also

You have been reading a chapter from
Unity 2018 Artificial Intelligence Cookbook - Second Edition
Published in: Aug 2018
Publisher:
ISBN-13: 9781788626170
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 $19.99/month. Cancel anytime
Banner background image