- Home /
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
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!
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.
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
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
Your answer
Follow this Question
Related Questions
Gestures in javascript 4 Answers
fps shooting in the direction of character main cam 1 Answer
Player cannot shoot 2 Answers