x


[Closed] Need a script for a gun that shoots bullet using raycast

As mentioned on top , need a script for a gun that shoots bullet using raycast.. Also, is there a way to make a bullet trail from a gun that uses raycast? Thanks!

more ▼

asked Jun 16 '10 at 09:55 AM

user-3061 (yahoo) gravatar image

user-3061 (yahoo)
126 29 29 37

I'm guessing this is the same person that asked in http://answers.unity3d.com/questions/12564/shooting-a-bullet-projectile-properly - if not, take a look at the answer given there.

Jun 16 '10 at 10:56 AM Marowi

i read the answer, but the effects are still same... any other suggestions? i cant get the bullet in the right track. It just shoot elsewhere

Jun 16 '10 at 11:31 AM user-3061 (yahoo)

Don't ask people to write scripts for you and don't ask duplicate questions just because you didn't get a freakin beautiful answer, it pisses me off.

Sep 10 '11 at 02:13 PM FTheCloud

This isn't UnityFreeScriptRequests. Do the FPS tutorial and/or read the docs.

Sep 10 '11 at 03:12 PM PrimeDerektive

Give him a break, and stop being pissy little school girls. When someone asks for a script, they're probably new to Unity Answers. The least you could have done was give him somewhere to start off, rather than being catty little bitches. I'm so sick of people attacking others just because they asked for a script. Grow the hell up.

Dec 15 '12 at 05:47 PM The Goliath
(comments are locked)
10|3000 characters needed characters left

The question has been closed Jul 24 '13 at 10:20 PM by clunk47 for the following reason:

Question was answered, correct answer was accepted.


2 answers: sort voted first

This is a excellent shoot script but i dont know how to make bullet trail using raycast, but this script is very useful

Shooting.js

var defaultHolePrefab : GameObject;
var metalHolePrefab : GameObject;
var woodHolePrefab : GameObject;
var brickHolePrefab : GameObject;
var dirtHolePrefab : GameObject;
var mudHolePrefab : GameObject;
var bloodHolePrefab : GameObject;
 
var defaultSparkPrefab : GameObject;
var metalSparkPrefab : GameObject;
var woodSparkPrefab : GameObject;
var brickSparkPrefab : GameObject;
var dirtSparkPrefab : GameObject;
var mudSparkPrefab : GameObject;
var bloodSparkPrefab : GameObject;
 
//Lights should be setup in a 3 point lighting system (Triangular setup) around the muzzle flash. Set Range to 1
var muzzleFlash : GameObject;
var muzzleOrangeLight : GameObject; //Very light orange, point light
var muzzleWhiteLight : GameObject;
var muzzleBlueLight : GameObject; //Very light blue, point light. Almost white
 
var shootAudio : AudioClip;
var reloadAudio : AudioClip;
 
var isPrimary : boolean = false; //Used strictly for the pick up script for proper ammo pick up or weapon replacement
var isSecondary : boolean = false; //Used strictly for the pick up script for proper ammo pick up or weapon replacement
 
var isSingleShot : boolean = false;
var isAutomatic : boolean = false;
var isBurst : boolean = false;
 
private var isAiming : boolean = false;
private var isReloading : boolean = false;
 
var CurAmmo : int;
var AmmoPerClip : int;
var AmmoLeft : int;
var MaxAmmo : int;
var Damage : int;
var Range : float = 1000;
var Force : float = 1000;
var ReloadTime : float = 3;
var shotSpread : float = 0.2;
var ShootSpeed : float = 0.15; //time between each shot. Lower the number, faster the burst
 
private var AmmoToLoad : int;
private var ShootTimer : float = 0;
private var ShootCooler : float = 0.3;
private var shotTime : float = 0; //Must always remain at 0
private var burstCount : float = 0; // Must always remain at 0
private var flashLength : float = 0.1; //MuzzleFlash Length
private var flashScale : float = 0.25; //Max MuzzleFlash Scale
private var NextShot : float;
 
function Start()
{
        CurAmmo = AmmoPerClip;
        AmmoLeft = MaxAmmo;
        NextShot = ShootSpeed;
        muzzleFlash.active = false;
        muzzleOrangeLight.active = false;
        muzzleWhiteLight.active = false;
        muzzleBlueLight.active = false;
}
 
function Update()
{
        if(AmmoLeft < 0)
        {
                CurAmmo += AmmoLeft;
                AmmoLeft = 0;
        }
        if(CurAmmo > AmmoPerClip)
        {
                CurAmmo = AmmoPerClip;
        }
        if(ShootTimer > 0)
        {
                ShootTimer -= Time.deltaTime;
        }
        if(ShootTimer > 0)
        {
                ShootTimer = 0;
        }
        if(Input.GetKeyDown(KeyCode.Mouse0) && isSingleShot)
        {
                if(ShootTimer == 0)
                {
                        if(CurAmmo > 0 && isAiming == false)
                        {
                                if(isReloading == false)
                                {
                                        HipShoot();
                                        PlayShootAudio();
                                        MuzzleFlash();
                                        ShotTimer = ShootCooler;
                                }
                        }
                        if(CurAmmo > 0 && isAiming == true)
                        {
                                if(isReloading == false)
                                {
                                        AimShoot();
                                        PlayShootAudio();
                                        MuzzleFlash();
                                        ShootTimer = ShootCooler;
                                }
                        }
                }
        }
        if(Input.GetKey(KeyCode.Mouse0) && isAutomatic)
        {
                if(CurAmmo > 0 && isAiming == false)
                {
                        shotTime += Time.deltaTime;
                        if(shotTime >= NextShot)
                        {
                                if(isReloading == false)
                                {
                                        NextShot += ShootSpeed;
                                        PlayShootAudio();
                                        MuzzleFlash();
                                        HipShoot();
                                }
                        }
                }
                if(CurAmmo > 0 && isAiming == true)
                {
                        shotTime += Time.deltaTime;
                        if(shotTime >= NextShot)
                        {
                                if(isReloading == false)
                                {
                                        NextShot += ShootSpeed;
                                        PlayShootAudio();
                                        MuzzleFlash();
                                        AimShoot();
                                }
                        }
                }
        }
        if(Input.GetKey(KeyCode.Mouse0) && isBurst && burstCount < 3)
        {
                if(CurAmmo > 0 && isAiming == false)
                {
                        shotTime += Time.deltaTime;
                        if(shotTime >= NextShot)
                        {
                                if(isReloading == false)
                                {
                                        NextShot += ShootSpeed;
                                        burstCount += 1;
                                        PlayShootAudio();
                                        MuzzleFlash();
                                        HipShoot();
                                }
                        }
                }
                if(CurAmmo > 0 && isAiming == true)
                {
                        shotTime += Time.deltaTime;
                        if(shotTime >= NextShot)
                        {
                                if(isReloading == false)
                                {
                                        NextShot += ShootSpeed;
                                        burstCount += 1;
                                        PlayShootAudio();
                                        MuzzleFlash();
                                        AimShoot();
                                }
                        }
                }
        }
        if(Input.GetKeyUp(KeyCode.Mouse0))
        {
                NextShot = 0;
                shotTime = 0;
                if(isBurst == true)
                {
                        burstCount = 0;
                }
        }
        if(Input.GetKeyDown(KeyCode.R) && CurAmmo > 0)
        {
                if(AmmoLeft > 0 && CurAmmo < AmmoPerClip)
                {
                        if(isReloading == false)
                        {
                                Reload();
                        }
                }
        }
        if(Input.GetKeyDown(KeyCode.Tab) && isAiming == false)
        {
                Aim();
        }
        if(Input.GetKeyUp(KeyCode.Tab))
        {
                Return();
        }
}
 
function HipShoot()
{
        var Hit : RaycastHit;
        var DirectionRay = SprayDirection();
        Debug.DrawRay(transform.position, DirectionRay * Range, Color.blue);
        if(Physics.Raycast(transform.position, DirectionRay, Hit, Range))
        {
                var hitRotation = Quaternion.FromToRotation(Vector3.up, Hit.normal);
                if(Hit.transform.tag == "Untagged")
                {
                        var defaultHole = Instantiate(defaultHolePrefab, Hit.point, hitRotation) as GameObject;
                        defaultHole.transform.parent = Hit.transform;
                        if(defaultSparkPrefab)
                        {
                                Instantiate(defaultSparkPrefab, Hit.point, hitRotation);
                        }
                }
                if(Hit.transform.tag == "Metal")
                {
                        var metalHole = Instantiate(metalHolePrefab, Hit.point, hitRotation) as GameObject;
                        metalHole.transform.parent = Hit.transform;
                        if(metalSparkPrefab)
                        {
                                Instantiate(metalSparkPrefab, Hit.point, hitRotation);
                        }
                }
                if(Hit.transform.tag == "Wood")
                {
                        var woodHole = Instantiate(woodHolePrefab, Hit.point, hitRotation) as GameObject;
                        woodHole.transform.parent = Hit.transform;
                        if(woodSparkPrefab)
                        {
                                Instantiate(woodSparkPrefab, Hit.point, hitRotation);
                        }
                }
                if(Hit.transform.tag == "Brick")
                {
                        var brickHole = Instantiate(dirtHolePrefab, Hit.point, hitRotation) as GameObject;
                        brickHole.transform.parent = Hit.transform;
                        if(brickSparkPrefab)
                        {
                                Instantiate(brickSparkPrefab, Hit.point, hitRotation);
                        }
                }
                if(Hit.transform.tag == "Dirt")
                {
                        var dirtHole = Instantiate(dirtHolePrefab, Hit.point, hitRotation) as GameObject;
                        dirtHole.transform.parent = Hit.transform;
                        if(dirtSparkPrefab)
                        {
                                Instantiate(dirtSparkPrefab, Hit.point, hitRotation);
                        }
                }
                if(Hit.transform.tag == "Mud")
                {
                        var mudHole = Instantiate(mudHolePrefab, Hit.point, hitRotation) as GameObject;
                        mudHole.transform.parent = Hit.transform;
                        if(mudSparkPrefab)
                        {
                                Instantiate(mudSparkPrefab, Hit.point, hitRotation);
                        }
                }
                if(Hit.transform.tag == "Enemy")
                {
                        var bloodHole = Instantiate(bloodHolePrefab, Hit.point, hitRotation) as GameObject;
                        bloodHolePrefab.transform.parent = Hit.transform;
                        if(bloodSparkPrefab)
                        {
                                Instantiate(bloodSparkPrefab, Hit.point, hitRotation);
                        }
                }
                if(Hit.rigidbody)
                {
                        Hit.rigidbody.AddForceAtPosition(DirectionRay * Force, Hit.point);
                        Hit.collider.SendMessageUpwards ("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
                }
        }
        CurAmmo --;
        AmmoToLoad ++;
        if(CurAmmo < 0)
        {
                CurAmmo = 0;
        }
        if(CurAmmo == 0 && AmmoLeft > 0)
        {
                Reload();
        }
}
 
function AimShoot()
{
        var Hit : RaycastHit;
        var DirectionRay = transform.TransformDirection(Vector3.forward);
        Debug.DrawRay(transform.position, DirectionRay * Range, Color.blue);
        if(Physics.Raycast(transform.position, DirectionRay, Hit, Range))
        {
                var hitRotation = Quaternion.FromToRotation(Vector3.up, Hit.normal);
                if(Hit.transform.tag == "Untagged")
                {
                        var defaultHole = Instantiate(defaultHolePrefab, Hit.point, hitRotation) as GameObject;
                        defaultHole.transform.parent = Hit.transform;
                        if(defaultSparkPrefab)
                        {
                                Instantiate(defaultSparkPrefab, Hit.point, hitRotation);
                        }
                }
                if(Hit.transform.tag == "Metal")
                {
                        var metalHole = Instantiate(metalHolePrefab, Hit.point, hitRotation) as GameObject;
                        metalHole.transform.parent = Hit.transform;
                        if(metalSparkPrefab)
                        {
                                Instantiate(metalSparkPrefab, Hit.point, hitRotation);
                        }
                }
                if(Hit.transform.tag == "Wood")
                {
                        var woodHole = Instantiate(woodHolePrefab, Hit.point, hitRotation) as GameObject;
                        metalHole.transform.parent = Hit.transform;
                        if(woodSparkPrefab)
                        {
                                Instantiate(woodSparkPrefab, Hit.point, hitRotation);
                        }
                }
                if(Hit.transform.tag == "Brick")
                {
                        var brickHole = Instantiate(dirtHolePrefab, Hit.point, hitRotation) as GameObject;
                        brickHole.transform.parent = Hit.transform;
                        if(brickSparkPrefab)
                        {
                                Instantiate(brickSparkPrefab, Hit.point, hitRotation);
                        }
                }
                if(Hit.transform.tag == "Dirt")
                {
                        var dirtHole = Instantiate(dirtHolePrefab, Hit.point, hitRotation) as GameObject;
                        dirtHole.transform.parent = Hit.transform;
                        if(dirtSparkPrefab)
                        {
                                Instantiate(dirtSparkPrefab, Hit.point, hitRotation);
                        }
                }
                if(Hit.transform.tag == "Mud")
                {
                        var mudHole = Instantiate(mudHolePrefab, Hit.point, hitRotation) as GameObject;
                        mudHole.transform.parent = Hit.transform;
                        if(mudSparkPrefab)
                        {
                                Instantiate(mudSparkPrefab, Hit.point, hitRotation);
                        }
                }
                if(Hit.transform.tag == "Enemy")
                {
                        var bloodHole = Instantiate(bloodHolePrefab, Hit.point, hitRotation) as GameObject;
                        bloodHolePrefab.transform.parent = Hit.transform;
                        if(bloodSparkPrefab)
                        {
                                Instantiate(bloodSparkPrefab, Hit.point, hitRotation);
                        }
                }
                if(Hit.rigidbody)
                {
                        Hit.rigidbody.AddForceAtPosition(DirectionRay * Force , Hit.point);
                        Hit.collider.SendMessageUpwards ("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
                }
        }
        CurAmmo --;
        AmmoToLoad ++;
        if(CurAmmo < 0)
        {
                CurAmmo = 0;
        }
        if(CurAmmo == 0 && AmmoLeft > 0)
        {
                Reload();
        }
}
 
function Reload()
{
        isReloading = true;
        audio.PlayOneShot(reloadAudio);
        //Add Reload Animation
        yield WaitForSeconds(ReloadTime);
        isReloading = false;
        CurAmmo += AmmoToLoad;
        AmmoLeft -= AmmoToLoad;
        AmmoToLoad = 0;
}
 
function SprayDirection()
{
        var sprayX = (1 - 2 * Random.value) * shotSpread;
        var sprayY = (1 - 2 * Random.value) * shotSpread;
        var sprayZ = 1.0;
        return transform.TransformDirection(Vector3(sprayX, sprayY, sprayZ));
}
 
function Aim()
{
        //Add Aim Animation
        isAiming = true;
}
 
function Return()
{
        //Add Reverse Aim Animation
        isAiming = false;
}
 
function PlayShootAudio()
{
        audio.PlayOneShot(shootAudio);
}
 
function MuzzleFlash()
{
        muzzleFlash.transform.eulerAngles.x = Random.Range(0.0, 15.0);
        muzzleFlash.transform.localScale.x = 1 + Random.Range(-flashScale, flashScale); //Resets the scale
        muzzleFlash.active = true;
        muzzleOrangeLight.active = true;
        muzzleWhiteLight.active = true;
        muzzleBlueLight.active = true;
        yield WaitForSeconds(flashLength);
        muzzleFlash.active = false;
        muzzleOrangeLight.active = false;
        muzzleWhiteLight.active = false;
        muzzleBlueLight.active = false;
}

Source: http://forum.armedunity.com/

more ▼

answered Jul 15 '12 at 12:32 AM

DevSpace gravatar image

DevSpace
66 2

Shouldn't this have a citation? or is it yours?

[edit:] Nice on the source on the bottom (sorry if I didn't notice it before).

Aug 25 '12 at 11:38 AM Matt Downey

I use a similiar script, But I think you made it way 2 long, why a function Aim if you only have 1 line in it. But you get a like cuz its awesome! And because it made me think!

Aug 25 '12 at 12:05 PM Hybris

the "Aim" is an optional function, if you have aim down animation of your weapon, you can setup and use.

Sep 01 '12 at 03:34 PM DevSpace

If you're instantiating an actual bullet prefab, just add Component> Effects> Trail Renderer to that prefab...

Dec 16 '12 at 02:20 AM clunk47
(comments are locked)
10|3000 characters needed characters left

Dude you have to do it yourself or else, it wont do what YOU want it to do, you cant debug, fix or update the script, and you should learn how to make the script instead. A complicated script like this will need to be created to work with your game setup. Instead search up tutorials on scripting a gun: http://www.youtube.com/results?search_query=unity+gun+tutorial&oq=unity+gun+tutorial&gs_l=youtube.3..0.5858.9043.0.9173.18.9.0.5.5.0.322.1954.0j4j4j1.9.0...0.0...1ac.1.fcLfAvawVbI

more ▼

answered Nov 01 '12 at 06:44 PM

MrLolEthan gravatar image

MrLolEthan
56 4 6 8

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

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:

x4378
x2390
x447
x409
x312

asked: Jun 16 '10 at 09:55 AM

Seen: 8949 times

Last Updated: Dec 16 '12 at 02:21 AM