x


Making a shotgun

I want to make a shotgun using raycasts. I have a script for a pistol and was wondering if I could edit it.

function Update () 
{
    pistolflash = GameObject.FindWithTag("pistolflash");
    // raycast settings
    var direction = transform.TransformDirection(Vector3.forward);
    var hit : RaycastHit;
    var localOffset = transform.position;
        // check to see if user has clicked
        if(Input.GetButtonDown("Fire1"))
        {
            // if so, did we hit anything?
            if (Physics.Raycast (localOffset, direction, hit, 400)) 
            {
                // just so we can see the raycast
                Debug.DrawLine (localOffset, hit.point, Color.cyan);
                // print to console to see if we have fired
                print("we have fired!");
                // send damage report to object
                hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
            }
            AudioSource.PlayClipAtPoint(gunshot,transform.position);
            Instantiate (muzzleflash, pistolflash.transform.position, transform.rotation);

        }
}
more ▼

asked May 27, 2011 at 09:30 AM

Max 4 gravatar image

Max 4
269 179 126 135

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Hi man,

First up you need to create a for loop to do multiple rays.

The simplest way to get a shotgun affect would be to add a random XY each the origin offset vector, but you may want to think about altering the ray direction by very small degrees instead. I'll provide the offset version for you as I don't have access to unity to test out the tricker rotation stuff:

var shotgunPellets : int = 7; //How many pellets are in each shot

for (var i = 0; i < shotgunPellets; i++){ //For each pellet create a random origin and fire

    var randomX : float = Random.Range(-1.0,1.0);
    var randomY : float = Random.Range(-1.0,1.0);
    localOffset.y += randomY;
    localOffset.X += randomX;

if (Physics.Raycast (localOffset, direction, hit, 400)) {
//Hit code
}

}

//Audio and muzzle flash code
more ▼

answered May 27, 2011 at 09:43 AM

demize2010 gravatar image

demize2010
1k 56 46 61

(comments are locked)
10|3000 characters needed characters left

Only difference between a gun and a shotgun, scripting wise, is that a shotgun casts more rays.

So if you want some spread to the rays from your shotgun you might be able to use something like this: Clicky

I havnt tested this script, nor do i know if it works, but it looks like something you could use :)

just add a random.range in the angle, and cast like, 9 rays or so.

more ▼

answered May 27, 2011 at 09:39 AM

Kacer gravatar image

Kacer
793 45 36 53

(comments are locked)
10|3000 characters needed characters left

I would appreciate a version of this that would produce a cone shaped delivery of raycasts rather than tube as it were. I am struggling to find a way to randomize a ray's direction within a cone of influence from an initial ray fired from my gun. I've been trying to wrap my head around this for quite some time!

more ▼

answered Mar 18, 2013 at 03:22 AM

RowleyBirkin gravatar image

RowleyBirkin
37 11 11 12

@rowleybirkin, this question is 2 years old, and what you are looking for is different anyway. So why don't you post this as a new question?

Mar 18, 2013 at 04:16 AM Chronos-L
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2713
x990
x9
x5

asked: May 27, 2011 at 09:30 AM

Seen: 3932 times

Last Updated: Mar 18, 2013 at 04:16 AM