Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 24, 2013 at 10:20 PM by clunk47 for the following reason:

Question was answered, correct answer was accepted.

avatar image
0
Question by user-3061 (yahoo) · Jun 16, 2010 at 09:55 AM · raycastbulletshootweapon

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!

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image · Jun 16, 2010 at 10:56 AM 0
Share

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.

avatar image user-3061 (yahoo) · Jun 16, 2010 at 11:31 AM 0
Share

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

avatar image FTheCloud · Sep 10, 2011 at 02:13 PM 1
Share

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.

avatar image PrimeDerektive · Sep 10, 2011 at 03:12 PM 2
Share

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

avatar image SirBedlam · Dec 15, 2012 at 05:47 PM 0
Share

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.

2 Replies

  • Sort: 
avatar image
3
Best Answer

Answer by DevSpace · Jul 15, 2012 at 12:32 AM

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/

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Matt-Downey · Aug 25, 2012 at 11:38 AM 0
Share

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).

avatar image Hybris · Aug 25, 2012 at 12:05 PM 0
Share

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!

avatar image DevSpace · Sep 01, 2012 at 03:34 PM 0
Share

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

avatar image clunk47 · Dec 16, 2012 at 02:20 AM 0
Share

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

avatar image
0

Answer by MrLolEthan · Nov 01, 2012 at 06:44 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem creating a 2D scroller shooting game 2 Answers

Reloader Weapon's Bullet? 0 Answers

Ray curve for bullet gravity effect, or any way to make it 1 Answer

Shoot with ray cast angle 1 Answer

Raycast shooting problems 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges