• 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
Question by Weirdo-Studios · Jul 10, 2013 at 04:43 PM · raycastfpsbulletsdecalmuzzleflash

Raycast Decals,Sound and MuzzleFlash not working too well

Ok so i have all my code but the decals dont show up and their is no sound or flash on shooting apart from when i don't hit anything (my level has no ceiling so i shoot upwards) the reload works fine but when i shoot at a wall not my crates (they have rigid bodies) then the timer for shooting gets ignored too... I really don't know why this wont work i have spent ages trying to get it working.

MY SHOOT SCRIPT (Pretty long yes, i know, awkward).

 var Damage : int = 20;
 var Range : float = 1000;
 var Force : float = 500;
 var Clips : int = 2;
 var BulletsPerClip : int = 12;
 var ReloadTime : float = 2;
 var BulletsLeft : int = 0;
 var ShootTimer : float = 0;
 var ShootCooler : float = 0.6;
 public var ShootAudio : AudioClip;
 public var ReloadAudio : AudioClip;
 var Aiming: boolean = false;
 var Gun : GameObject;
 var HitParticles : ParticleEmitter;
 var MuzzleFlash : ParticleEmitter;
 var MuzzleCooler : float = 0.6;
 var MuzzleTimer : float;
 var Light1 : GameObject;
 var Light2 : GameObject;
 var Light3 : GameObject;
 var MuzzleSpeed : int = 200000;
 var Decals : GameObject;
 var DecalDis : float = 0.01;
 var hit = RaycastHit;
 
 
 function Start(){
 
     MuzzleFlash.emit = false;
     HitParticles.emit = false;
     BulletsLeft = BulletsPerClip;
     Gun.animation["Reload"].layer = 1;
     
     
 
 }
 
 
 function Update () {
 
     
     if (MuzzleTimer > 0){
         MuzzleTimer -= Time.deltaTime;
         MuzzleFlashShow();
     }
     
     if(MuzzleTimer < 0){
         MuzzleTimer = 0;
     }
     
     if (ShootTimer > 0){
         ShootTimer -= Time.deltaTime;
     }
     
     if(ShootTimer < 0){
         ShootTimer = 0;
     }
         
     if(Input.GetMouseButtonDown(0) && BulletsLeft)
     {
         if(ShootTimer ==0){
             RayShoot();
             PlayShootAudio();
             ShootTimer = ShootCooler;
         }
         
         if(MuzzleTimer ==0){
             MuzzleTimer = MuzzleCooler;
             MuzzleFlashShow();
          }
     }
 
     
     if (Input.GetMouseButtonUp(1))
     {
         Aiming = false;
     }
     
         
         if (Input.GetMouseButtonDown(1))
     {
         Aiming = true;
     }
     
 
 }
 
 function RayShoot () {
     //check aim
     if(Aiming == true){
         Gun.animation.Play("ShootingAiming");
     }
     //check aim
     else if(Aiming == false){
     
         Gun.animation.Play("ShootingIdle");
     }
     //take bullet
     BulletsLeft --;
     
     //check bulletleft
     if(BulletsLeft <0){
         BulletsLeft = 0;
     }
     
     //check bulletleft
     if(BulletsLeft == 0){
         Reload();
     }
     
     var Hit : RaycastHit;
     var DirectionRay : Vector3 = transform.TransformDirection(Vector3.forward);
     
     //debug
     Debug.DrawRay(transform.position, DirectionRay * Range, Color.blue);
     
     //if contact
     if(Physics.Raycast(transform.position , DirectionRay , Hit , Range)){
         
         //if it's rigidbody
         if(Hit.rigidbody) {
                 
                 //particle effects
                 HitParticles.transform.position = Hit.point;
                 HitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, Hit.normal);
                 HitParticles.Emit();    
                 
                 //force and dmg
                 Hit.rigidbody.AddForceAtPosition(DirectionRay * Force, Hit.point);
                 Hit.collider.SendMessageUpwards("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
         }
     
         
         if(Hit.collider){
             //spawn decal
             Instantiate(Decals, Hit.point +(Hit.normal * DecalDis),hit.Rotation);
         
             //particle effects
             HitParticles.transform.position = Hit.point;
             HitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, Hit.normal);
             HitParticles.Emit();    
         }
 
 }
 }
 
 
 function Reload() {
     PlayReloadAudio();
     Gun.animation.CrossFade("Reload", 0.4);
     yield WaitForSeconds(ReloadTime);
     if(Clips > 0){
     
         BulletsLeft = BulletsPerClip;
     }
 }
 
 function PlayShootAudio(){
     audio.PlayOneShot(ShootAudio);
 }
 
 function PlayReloadAudio(){
     audio.PlayOneShot(ReloadAudio);
 }
 
 function MuzzleFlashShow(){
     if(MuzzleTimer > 0){
         MuzzleFlash.emit = false;
         Light1.active = false;
         Light2.active = false;
         Light3.active = false;
     }
 
     if(MuzzleTimer == MuzzleCooler){
         MuzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360 * MuzzleSpeed, Vector3.forward);
         
         MuzzleFlash.emit = true;
         Light1.active = true;
         Light2.active = true;
         Light3.active = true;
     }
     
     
 
 }
Comment

People who like this

0 Show 0
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 People are following this question.

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

Related Questions

Instantiated bullet hole has wrong rotation 1 Answer

issue with bullet hole decal 1 Answer

Multiplayer FPS Bullets: should they be rigid bodies or raycast? 1 Answer

2D Bullet texture follow ray and Bullet Holes 1 Answer

fps shooting enemy 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