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 bowman290 · May 07, 2012 at 10:49 AM · javascriptfpsshootingbooleankinect

Shooting only once, returning boolean as false

Hi all, Basically i'm using Kinect and gesture recognition in my game and have a raycast shooting script on the player controlled by a "leftClick" gesture that i call from a script called shadow manager using a boolean called "areWeShooting". If areWeShooting is returned true then the player shoots, but because the boolean is marked true the player continually shoots. My assumption is that i should be able to return it as false after the if statement is completed by my heads somewhat cloudy at the minute and i can't think. I also suspect it could be that I'm doing this in an Update (checking every frame). I'm also aware that and "if" checking for boolean is not the easiest way around but given the guidelines i have yo use while using Kinect it's the only way i could conceive doing it. Sorry for any typos etc, long night / morning.

Script Follows:

 #pragma strict
 var findGestures : GameObject;
 
 var Range : float = 1000;
 var Force : float = 1000;
 var Clips : int = 20;
 var BulletPerClip : int = 60;
 var ReloadTime : float = 3.3;
 var Damage : int = 10;
 var BulletsLeft : int = 0;
 var ShootTimer : float = 0;
 var ShootCooler : float = 0.9;
 public var ShootAudio: AudioClip;
 public var ReloadAudio: AudioClip;
 public var hitLayer : LayerMask;
 
 
 function Start () {
 
     BulletsLeft = BulletPerClip;
 
 }
 
 function Update () {
 
     if( ShootTimer > 0)
     {
     
         ShootTimer -= Time.deltaTime;
     
     }
     
     if( ShootTimer < 0)
     {
     
         ShootTimer = 0;
     
     }
 
     if(findGestures.GetComponent(ShadowManager).areWeShooting == true){
         if( ShootTimer == 0)
         {
     
         PlayShootAudio();
         RayShoot();
         
         ShootTimer = ShootCooler;
         
         }
     }
 }
 
 function RayShoot(){
 
     //add an animation for shooting
     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))
     {
         if (Hit.rigidbody)
         {
         
             Hit.rigidbody.AddForceAtPosition(DirectionRay * Force, Hit.point);
             
             Hit.collider.SendMessageUpwards("ApplyDamage" , Damage, SendMessageOptions.DontRequireReceiver);
         }
         
         else if(Hit.collider.gameObject.tag == 'Enemy')
         {
             Hit.collider.GetComponent(EnemyHealth).takeDamage();
             print("StepONE");
         
         }
         
     }
     
     BulletsLeft --;
     
     if(BulletsLeft < 0)
     {
     
     BulletsLeft = 0;
     
     }
     
     if(BulletsLeft == 0)
     {
     
     Reload();
     
     
     }
 
 }
 
 
 
 function Reload(){
 
     PlayReloadAudio();
     //Play animation for reload
     yield WaitForSeconds(ReloadTime);
     
         if (Clips > 0)
         {
         
             BulletsLeft = BulletPerClip;
         
         }
 
 }
 
 
 function PlayShootAudio(){
 
     audio.PlayOneShot (ShootAudio);
 
 
 }
 
 function PlayReloadAudio(){
 
     audio.PlayOneShot (ReloadAudio);
 
 }


The ShadowManager script is quite long and references a lot of things that aren't applicable but i know to call a gesture I declare the boolean:

 public bool areWeShooting = false;

and then i call the gesture and mark the bool true if we leftClick

if (isGestureActive("leftClick",0) == true) { areWeShooting = true;
}

I'm not sure if i have to call another gesture like "idle" and then mark areWeShooting as false or i can call a single instance of leftClick and then re-mark the bool as false afterwards.

Sorry if i've not made myself clear with the extra info

Any help is much appreciated, Kev

Comment
Add comment · Show 1
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 syclamoth · May 07, 2012 at 05:21 PM 0
Share

Right. In your 'Shadow$$anonymous$$anager' script, you need to either reset all the flags in LateUpdate, or change the whole thing so that it uses a callback-like system. As it stands, your gesture never resets- so as soon as the player does the gesture once, the character in-game will continue shooting forever!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by syclamoth · May 07, 2012 at 11:30 AM

Just include a 'cooldown' variable as well- it's pretty simple.

At the top, include these two variables:

 private var canShoot : boolean;
 var cooldownTime : float;

Then, in your if statement, add this:

 if(findGestures.GetComponent(ShadowManager).areWeShooting && canShoot)

Then, make a call to this function every time you fire:

 function RunCooldown()
 {
     canShoot = false;
     yield WaitForSeconds(cooldownTime);
     canShoot = true;
 }

Of course, it seems that you are already doing something similar- Unfortunately, you've shown us the wrong script if the problem is in the other one!

If this isn't the problem at all, edit your post to include the 'ShadowManager' code. You need to reset the boolean value in there for when they player isn't shooting.

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 bowman290 · May 07, 2012 at 12:54 PM 0
Share

Thanks for your time, I'll look at your answer and get back to you shortly, just thought I'd let you know the question has been updated with a little bit of extra info regarding the shadow $$anonymous$$anager. Regards, $$anonymous$$ev

avatar image
0

Answer by bowman290 · May 08, 2012 at 02:39 AM

Sorry, i came back with fresh eyes and realised i was being reeeeaaaallly stupid. All i needed was an else statement on my shadow managers areWeWalking so that it reset itself to false after becoming active... derp

 if (isGestureActive("leftClick",0) == true)
                     {
                         areWeShooting = true;
                         
                     }
     else 
         {
             areWeShooting = false;
                         
         } 

I'm blaming sleep dep :) Anyway, thanks for your help and time!

Regards, Kev

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Gestures in javascript 4 Answers

fps shooting in the direction of character main cam 1 Answer

Player cannot shoot 2 Answers

Sending the C# function parameter to JS function. 1 Answer

Need help... Start Button no longer working!!!!! 0 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