You can instantiate prefabsAn asset type that allows you to store a GameObject complete with components and properties. The prefab acts as a template from which you can create new object instances in the scene. More info
See in Glossary to use as projectiles and destroy them with explosion effects in your application.
The following example instantiates a projectile prefab when the user presses the fire button. You can attach it to a GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary which acts as a launcher for the prefab.
Projectile
and then drag it into the Assets
folder of your project to create a prefab asset.You can optionally add a textureAn image used when rendering a GameObject, Sprite, or UI element. Textures are often applied to the surface of a mesh to give it visual detail. More info
See in Glossary to the prefab, change its dimensions, or import a different modelA 3D model representation of an object, such as a character, a building, or a piece of furniture. More info
See in Glossary to act as the projectile.
To add an explosion to the projectile prefab, you must have a prefab asset that represents an explosion. You can use the particle systemA component that simulates fluid entities such as liquids, clouds and flames by generating and animating large numbers of small 2D images in the scene. More info
See in Glossary to create a prefab asset, or find an explosion effect on the Asset StoreA growing library of free and commercial assets created by Unity and members of the community. Offers a wide variety of assets, from textures, models and animations to whole project examples, tutorials and Editor extensions. More info
See in Glossary and add it to the Assets
folder of your project.
Then create a script called Projectile
as follows:
using UnityEngine;
public class Projectile : MonoBehaviour
{
public GameObject explosion;
void Start()
{
// Deletes the projectile after 10 seconds, regardless
// of whether it collided with anything. This prevents
// instances from staying in the scene indefinitely.
Destroy(gameObject,10);
}
void OnCollisionEnter()
{
// When the projectile hits something, create an explosion
// and remove the projectile.
Instantiate(explosion,transform.position,transform.rotation);
Destroy(gameObject);
}
}
The script instantiates the explosion at the projectile’s current position and removes the projectile GameObject when the projectile collides with something.
To use the script, attach it to the projectile prefab asset:
Projectile
prefab asset and open it in prefab editing mode.Projectile
script onto it.To launch the projectiles, you need to create a script that instantiates projectiles when the fire key is pressed, and add that script to a GameObject.
Create a script called FireProjectile
and add the following contents to it:
using UnityEngine;
using UnityEngine.InputSystem;
public class FireProjectile : MonoBehaviour
{
// Only GameObjects with a Rigidbody can be assigned as the projectile.
public Rigidbody projectile;
// Speed of the projectile when fired.
// This is a public variable so it can be adjusted in the Unity Editor.
public float speed = 4;
// Update is called once per frame
// This method checks for input and fires a projectile if the attack action is pressed.
void Update()
{
// Check if the "Attack" action was pressed this frame
// If it was, instantiate a projectile at the player's position and set its velocity.
if (InputSystem.actions.FindAction("Attack").WasPressedThisFrame())
{
Rigidbody p = Instantiate(projectile, transform.position, transform.rotation);
p.linearVelocity = transform.forward * speed;
}
}
}
This script uses Instantiate
to launch a projectile. When making a public prefab variable, the variable type can be a GameObject, or it can be any valid component type (either a built-in Unity component or one of your own MonoBehaviour scripts).
For component type variables (such as Rigidbody, Collider, and Light), you can only assign GameObjects of that component type to the variable, and the Instantiate
function returns a reference to that specific component on the new GameObject instance.
You must attach the script to a GameObject to use it. To do so:
FireProjectile
script onto the cube.Projectile
prefab asset into the Projectile field of the Fire Projectile script.The cube fires the sphere projectiles and the explosion happens when they collide with the ground.
Note that any instantiated objects appear in the Hierarchy with (Clone)
appended to the name.