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 ...

Get Unity 2018 Artificial Intelligence Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.