Raycast Force and Ejecting Bullet Shells in Unity

Sharing is caring:

Impact Forces and Ejecting Bullet Shells

In this Unity tutorial we’ll be showing you how to apply force to rigidbodies in our game with our raycast. We’ll also go over how to “eject” bullet shells from our gun. We’ll start off by modifying our previous raycast script by using “Add Force” which will apply a force to wherever we left click.  We’ll test this out on some cubes that have rigidbodies.

We can adjust the force behind our raycast in the inspector panel. To add some more realism we’ll create a bullet eject script that will instantiate a bullet shell prefab from the side of our gun. We’ll instantiate the bullet shells when we left click to match our shooting script. Every time we fire we’ll instantiate one bullet shell.

Create a bullet shell from a small cylinder, add a yellow/bronze like material to it just to make it look somewhat like a bullet casing. Make sure it has a rigidbody attached to it. Then create an empty game object and place it near the “eject” portion of the gun. Apply the “Eject” script to this empty game object and plug in the bullet shell prefab into this script. Below are the scripts used in this video:

 

Javascript Files

Eject.js (apply this to the empty game object that will eject bullet shells)


var bulletCasing : Rigidbody;

var ejectSpeed : int = 10;

function Start () {

}

function Update () {

if (Input.GetButton("Fire1")) {

var clone : Rigidbody;

clone = Instantiate(bulletCasing, transform.position, transform.rotation);

clone.velocity = transform.TransformDirection(Vector3.left * ejectSpeed);

}

}

Raycast.js (apply this to the empty game object in front of the barrel of our gun)


var bulletTex : GameObject[]; // creates an array to use random textures of bullet holes

var power : int = 10; //provides power to our raycast that can affect our rigidbody components

function Start () {

}

function Update () {

var fwd = transform.TransformDirection(Vector3.forward); //casts our raycast in the forward direction

var hit : RaycastHit;

Debug.DrawRay(transform.position, fwd * 10, Color.green); //drays our raycast and gives it a green color and a length of 10 meters

if(Input.GetButtonDown ("Fire1") && Physics.Raycast(transform.position, fwd, hit, 10)){ //when we left click and our raycast hits something

Instantiate(bulletTex[Random.Range(0,3)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)); //then we'll instantiate a random bullet hole texture from our array and apply it where we click and adjust

// the position and rotation of textures to match the object being hit

if (hit.rigidbody !=null)

hit.rigidbody.AddForceAtPosition(fwd * power, hit.point);  //applies a force to a rigidbody when we click on it. Multiples our forward raycast times our power variable at the position we click

}

}

 

Jonathan Gonzalez

I love all things in game development and want to contribute by teaching others how to create games. I'm always looking to help others create awesome digital worlds. I've been a hardcore gamer since I was a kid and now it's my turn to create awesome digital experiences.

More Posts - Website

Follow Me:
TwitterYouTube

Comments

  1. Henry Smith says:

    Recoil?

Speak Your Mind

*