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 /
avatar image
0
Question by Kevlud · Jul 14, 2012 at 10:25 AM · raycastshootdrawray

RayCast "blueline" qoes down

Hallo. I want to create FPS. I was finding gun shoot script for 2 days. I found narutoisgreat1234's Raycast shoot script. Here is it with my small modifications. :`

var Range : float = 1000;

  var Force : float = 1000;
  
  var Clips : int = 20;
  
  var BulletPerClip : int = 60;
  
  var RelaodTime : float = 3.3;
  
     var Damage : int = 10;
     
  var MoveSpeed: float = 1;
  
  var BulletsLeft : int = 0;
  
  var ShootTimer : float = 0;
  
  
  
  var ShootCooler : float = 0.5;
  
  public var ShootAudio : AudioClip;
  
  public var ReloadAudio : AudioClip;
  
  public var muzzleFlash : ParticleEmitter;
  
  public var hitParticles : ParticleEmitter;
  
  public var muzzleFlashTimer : float = 0;
  
  public var KeyCooler : float = 0.5;
  
     public var KeyTimer : float = 0;
  
  public var muzzleFlashCooler : float = 0.5;
  
  public var Lights1 : GameObject;
  
  public var Lights2 : GameObject ;
  
  public var Lights3 : GameObject;
  
 
 function Start(){
 
  BulletsLeft = BulletPerClip;
  
  
  muzzleFlash.emit = false;
  
  
  DefaultPos = transform.localPosition;
  
  hitParticles.emit = false;
  
      
 }
 
 
 
 
 function Update () {
 
 
 if( KeyTimer > 0){
 
      KeyTimer -= Time.deltaTime;
    
 }
 
 if( KeyTimer < 0){
 
      KeyTimer= 0;
    
 }
 
 
 if( muzzleFlashTimer > 0){
 
      muzzleFlashTimer -= Time.deltaTime;
   MuzzleFlash();
 }
 
 
 if( muzzleFlashTimer < 0){
 
      muzzleFlashTimer = 0;
     
 
 }
   
 if( ShootTimer > 0){
             
              ShootTimer -= Time.deltaTime;
    }
    
    if(ShootTimer < 0){
                    
                    ShootTimer = 0;
                   
                   }
                   
              if( KeyTimer == 0){
  if(Input.GetMouseButton(0) && BulletsLeft ){
  
  if( ShootTimer  == 0){
  
  PlayShootAudio();
  
  
                 RayShoot();
                  
                 
                    
                 ShootTimer  = ShootCooler;
                 KeyTimer = KeyCooler;
                 
                 }
                 
  if( muzzleFlashTimer == 0){
       
                   muzzleFlashTimer = muzzleFlashCooler;   
                         MuzzleFlash ();    
 } 
 }
 
 
 }
 }
 
 function MuzzleFlash (){
 
 if( muzzleFlashTimer > 0){
 
           
         muzzleFlash.emit = false;
            
            Lights1.active = false;
            Lights2.active = false;
            Lights3.active = false;
               
 
 
 }
 if( muzzleFlashTimer == muzzleFlashCooler){
   
       muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360 * 2000000, Vector3.forward);
 
            muzzleFlash.emit = true;
            
             Lights1.active = true;
            Lights2.active = true;
            Lights3.active = true;
 }
 }
 
 
 function RayShoot (){
 
 
  
    //GameObject.Find("m1911pistoleReloadandShooting").animation.Play("Shoot");  //(if your player has animations then remove the // infront of the GameObject.Find and then 
         //(replace your objetcs name there. and then the animation name)
  var hit : RaycastHit;
 
  
  var direction : Vector3  = transform.TransformDirection( Vector3.forward);
          
  Debug.DrawRay(transform.position , direction * Range , Color.blue);
  
  if(Physics.Raycast(transform.position , direction , hit, Range)){
  var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
  
  if(hit.rigidbody){
  
  if( hitParticles){
  
  hitParticles.transform.position = hit.point;
  
  hitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
  hitParticles.Emit();
 
    
      hit.rigidbody.AddForceAtPosition( direction * Force , hit.point);
      hit.collider.SendMessageUpwards("ApplyDamage" , Damage , SendMessageOptions.DontRequireReceiver);
  }
  }
  }
  
  
      
  
  
  BulletsLeft --;
  
  
  if(BulletsLeft < 0){
  
  BulletsLeft = 0;
  
  }
  
  if( BulletsLeft == 0 ){
  
  Reload();
  
  
                 }
 
  }
     
 
 
 function Reload (){
 
  PlayReloadAudio();
  yield WaitForSeconds(0.2);
  
  //GameObject.Find("m1911pistoleReloadandShooting").animation.Play("Reload"); //(if your player has animations then remove the // infront of the GameObject.Find and then 
               //(replace your objetcs name there. and then the animation name)
      yield WaitForSeconds( RelaodTime);
      
       if(Clips > 0){
       
                 BulletsLeft = BulletPerClip; 
                 
                     Clips -= 1;    
       
 }
 }
 
 
 function PlayShootAudio(){
 
  audio.PlayOneShot( ShootAudio);
 
 }
 
 function PlayReloadAudio(){
 
   audio.PlayOneShot( ReloadAudio);
 
 
 }

` Now what's my problem. When i shoot, drawray (debug blue line) goes down and i can't interact with others things because drawray is down. Thanks in advance !

Comment
Add comment · Show 8
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 whydoidoit · Jul 14, 2012 at 10:26 AM 0
Share

Can you edit your question and format the code by indenting it 4 spaces or 1 tab before pasting - it's unreadable like that.

avatar image whydoidoit · Jul 14, 2012 at 10:31 AM 0
Share

@OrangeLightning - formatting the code is horrible now isn't it? Since UDN suddenly all the damn symbols are escaped...

avatar image Hybris · Jul 14, 2012 at 11:37 AM 0
Share

Its probably something with parenting I got the problem all the time, you should fiddle with the parents of the gun, or attach the script to another gameobject.

avatar image Kevlud · Jul 14, 2012 at 12:14 PM 0
Share

@Hybris -> Ok i will try that. Did you solved it in your project? BTW ALL: Sorry for my bad formating and thank for editting !

avatar image Hybris · Jul 14, 2012 at 01:33 PM 0
Share

I did solve it, BTW sometimes the drawray doesnt work, then you just have to delete the drawray, and it'll work

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DevSpace · Jul 14, 2012 at 11:45 PM

This is a very improved version of your script with many new features like bullet holes and sparks and mode like automatic, burst and single shot. if the ray keep going down rotate your weapon because the ray only goes to Z, and if your Z is set to down the weapon shoot the ray to down every time.

In this script, if the shoot goes to back change

var sprayZ = 1.0;
to
var sprayZ = -1.0;
.

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; }

Comment
Add comment · Show 1 · 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 Hybris · Aug 25, 2012 at 12:09 PM 0
Share

$$anonymous$$ight I advise you not to just paste your scripts in a answer, the point is that they learn not copy paste scripts.

Your answer

Hint: You can notify a user about this post by typing @username

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

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

Need a script for a gun that shoots bullet using raycast 2 Answers

Script keeps functioning when disabled 1 Answer

RayCast Problem : 2d 1 Answer

Help with making item invisible if not on screen 1 Answer

Scripted Door w/ Line of Sight 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