Shoot a projectile with Instantiate

The Shoot a projectile tutorial teaches you how to press a button to Instantiate a prefab game object projectile and apply physics forces to it to to trigger a script on impact.

Instantiating objects and shooting projectiles are two of the most commonly used concepts in game development.

To shoot a projectile, use this script from pastebin called Shoot:

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

public class Shoot : MonoBehaviour
 {
 public KeyCode shootKey = KeyCode.F;
 public GameObject projectile;
 public float shootForce;

// Use this for initialization
 void Start () {

}

// Update is called once per frame
 void Update ()
 {
 if (Input.GetKeyDown(shootKey))
 {
 GameObject shot = GameObject.Instantiate(projectile, transform.position, transform.rotation);
 shot.GetComponent<Rigidbody>().AddForce(transform.forward * shootForce);
 }
 }
 }
  1. In Project, Create a C# Script
  2. Name it Shoot (It will actually be called Shoot.cs)
  3. Open it
  4. Erase the code
  5. Paste in the code from pastebin
  6. Under your Player object which might be called FPSCharacter, there is a triangle
  7. Click the triangle to expand the camera which is probably called FirstPersonCharacter
  8. Rename it Camera
  9. Create an empty object under the camera
  10. Name it “Gun”
  11. Attach the Shoot.cs script
  12. Change shoot force to 5000.
  13. Create a sphere
  14. Rename it “Projectile”
  15. In Inspector, Add Component > Physics > Rigidbody
  16. Drag the Projectile into your project to make it a prefab
  17. Click on the Gun object and drag the Projectile prefab from Project into the Projectile variable field in the inspector
  18. Save
  19. Click Play to test if hitting F key shoots the projectile out.
  20. If you have script errors in the Console, double check that the code is correct and that the script is named Shoot(Shoot.cs) to match the class name in the file.

Create a Projectile script to make the projectiles break blocks

Use the following Projectile.cs script on the projectiles to make them hit blocks and delete them.

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

public class Projectile : MonoBehaviour {

void OnCollisionEnter(Collision collision)
 {
 if (collision.gameObject.tag == "Block")
 {
 GameObject.Destroy(collision.gameObject);

GameOBject.Destroy(gameObject);
 }
 }
 }

Troubleshooting

What if your projectiles go in the wrong direction?

Make sure you are using velocity relative to the camera direction, not an arbitrary direction.

What if your projectiles hit your character as they shoot?

Use Physics.IgnoreRaycastCollision().

Next steps: Shoot a raycast

Unity has an excellent tutorial about shooting with raycasts.

raycasts.png

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s