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 Furdak · Nov 03, 2011 at 11:16 PM ·

Need a bit of help with my script (about 100 lines)

The below is my code, I have a few problems with it:

First off, When I left click, no sound happens (yes sound files are attached to the value) And I would like to know how to make the ray shoot in the instance of me left clicking and not having it be as long as I hold down the trigger

Secondly, the reload sound happens every two seconds, when It should happen every 0.7 seconds.

Thirdly, I don't know how but I would like the reloading sequence to repeat it's self until it is false so that if I have say three rounds left inside the mag, it goes *Click *Click and stop there since the value of the ammo is equal to 5.

Lastly, I have no Idea of how to have a cool down time between every shot. So any help would be welcome. I have the var already

If you happen to find other issues with my code please speak of it, Thanks

*BTW I have a ray origin and a Sound origin allready

    var Range : float = 250;
    
    var Force : float = 1000;
 
    var Ammo : int = 60;
    
    var BulletsPerClip : int = 5;
    
    var ReloadTime : float = 0.7;
    
    var BulletsLeft : int = 0;
    
    var ShootTimer : float = 0;
    
    var ShootCooler : float = 1;
    
    public var ShootAudio : AudioClip;
    
    public var ReloadAudio : AudioClip;
    
    public var ArmAudio : AudioClip;
 
 
 function Start (){
    BulletsLeft = BulletsPerClip;
 }
 
 function Update () {
    if(Input.GetKey( KeyCode.Mouse0 )){
        RayShoot ();
    }
 }
    
      
           
                
                     
 function RayShoot () {
    var Hit : RaycastHit;
    var DirectionRay = transform.TransformDirection( Vector3.forward );
    Debug.DrawRay( transform.position , DirectionRay * Range , Color.red );
             
    if ( BulletsLeft < 0 )
            
       if(Physics.Raycast( transform.position , DirectionRay , Hit , Range ));
             
             if( Hit.rigidbody ){
             
             Hit.rigidbody.AddForceAtPosition( DirectionRay * Force , Hit.point );
             
             audio. PlayOneShot (ShootAudio);
             
             }
             
             }
             
             BulletsLeft --;
             
             if( BulletsLeft < 0){
                 BulletsLeft = 0;
             if( BulletsLeft ==0 ){
                 Reload ();
             }
 }
             
 function Reload (){
    if( Input.GetKey( KeyCode.R ));
    yield WaitForSeconds( ReloadTime );
    if( BulletsLeft < BulletsPerClip );
        BulletsLeft ++ ;
    audio. PlayOneShot (ReloadAudio);
    yield WaitForSeconds( ReloadTime );
    if( BulletsLeft < BulletsPerClip );
        BulletsLeft ++ ;
    audio. PlayOneShot (ReloadAudio); 
    yield WaitForSeconds( ReloadTime );
    if( BulletsLeft < BulletsPerClip );
        BulletsLeft ++ ;
    audio. PlayOneShot (ReloadAudio);
    yield WaitForSeconds( ReloadTime );
    if( BulletsLeft < BulletsPerClip );
        BulletsLeft ++ ;
    audio. PlayOneShot (ReloadAudio);
    yield WaitForSeconds( ReloadTime );
    if( BulletsLeft < BulletsPerClip );
        BulletsLeft ++ ;
    audio. PlayOneShot (ReloadAudio);
    yield WaitForSeconds( ReloadTime );
    if( BulletsLeft < BulletsPerClip );
        BulletsLeft ++ ;
    audio. PlayOneShot (ReloadAudio);
    audio. PlayOneShot (ArmAudio);
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by syclamoth · Nov 03, 2011 at 11:41 PM

Well, there are multiple issues here, so let me deal with them one by one from the top.

ONE: if you use

 if(Input.GetKeyDown( KeyCode.Mouse0 )){

instead of

 if(Input.GetKey( KeyCode.Mouse0 )){

it'll only trigger when the player first puts their finger on the button.

TWO: Inside your 'RayHit' function, you have an if statement which doesn't end in an open-close curly bracket, it instead ends in a semicolon. While this is syntactically correct, it is unlikely to do what you want it to (because there's no point having an if statement without some code for it to trigger). Get rid of the semicolon at the end of

 if(Physics.Raycast( transform.position , DirectionRay , Hit , Range ));

(so)

 if(Physics.Raycast( transform.position , DirectionRay , Hit , Range )){
     // Do some hit mechanics here
 }

THREE: I would put the bullet depletion and reloading stuff outside of the RaycastHit if statemend discussed above, because otherwise you don't lose any ammo from shooting into the sky. Also play the audio at that point.

FOUR: What's the 'if( Input.GetKey( KeyCode.R ));' doing at the top of your reload function? Not only is it another non-functional if statement, in this case I don't think you want it at all! Get rid of that line, and replace it with something in your Update Loop which allows you to manually start reloading.

(reloading is a boolean value defined at the top- you'll have to add it. It stops the player from shooting or attempting to reload while the gun is still being loaded. You may want to have some kind of override for this, if it's a bolt-action weapon and you want to be able to interrupt the reload action, at the cost of having a smaller clip)

 function Update () {
     if(reloading)
     {
         if(Input.GetKeyDown( KeyCode.Mouse0 )){
             RayShoot ();
         }
         if(Input.GetKeyDown(KeyCode.R))
         {
             Reload();
         }
     }
 }

FIVE: Your reload function has a lot of reused code, and could be simplified a lot. At the moment, it will always try to reload exactly 6 bullets, and doesn't check for if the clip is full or not before going on to the next reload step. It'd be better to do something like

 while(bulletsLeft < bulletsPerClip)
 {
     audio.PlayOneShot (ReloadAudio);
     yield WaitForSeconds( ReloadTime );
     bulletsLeft++;
 }
 audio.PlayOneShot (ArmAudio);

SIX: Please, make all of your member names lower-case. It seems like nit-picking, but the time will come when you can't tell the difference between a type and a member, and that'll cost you.

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 Furdak · Nov 04, 2011 at 12:06 AM 0
Share

Cool thanks, helped a lot.

That looping stuff is tricky.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Vehicle Script Debugging 1 Answer

How to make the trigger work only once. (SOUND) 1 Answer

Scripts stop working in Maximize On View and testing on Android 0 Answers

(C#)How To Disable Gravity From Script? 4 Answers


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