- Home /
Shot targets (FindGameObjectWithTag) don't add to "Total Targets Shot" float?
Okay so in order to get this to work I've been adding variable upon variable upon variable... obviously this does nothing unless implemented correctly. The scenario: six targets lined up in a shooting range. If a gameobject with the tag "Bullet" enters one of their BoxColliders, then the script is supposed to add one point (1) to the TargetsShot float, and then send that number to the GUI text. It sends the text alright, that's no problem. BUT: when I shoot a target with the bullet tagged as "Bullet", it does not add 1. In some cases, it adds something like .2, and once all of the targets are shot, it finally ends up being "Targets Shot: 1/6". Something with my math is wrong, and I'm trying to figure out what's happening. Also, it has a hard time detecting all of the bullets at once, so if I shoot a spread of say 6 bullets, it only knows about one of them. BTW, the "1" in "1/6" never goes higher than that.
The animation script for a shot target also deactivates the BoxColliders, preventing glitches after they're shot (like continuing to play the animation by shooting it). I used this fact as a boolean. If the boxcollider is deactivated, then add one point. Pretty simple right? Nope.
These are literal targets that fall over when you shoot them. So why isn't the script doing its job?
Really sorry about the mess.. I was experimenting with different variables. If a variable doesn't seem like it does anything for the script, then just ignore it...
Can anyone think of any alternative to using the Box Collider to do what I want?
My code:
#pragma strict
var SoundLocation : GameObject;//3D Location of the 3D sound effect
var Bullet; //The GameObject tagged "Bullet"... not really implemented
var TargetAnim : String = "TargetShot"; //The animation for the Target.
var pickup : AudioClip;//Sound after a Target has been shot
var TargetShot : boolean = false; // If a Bullet with the tag "Bullet" enteres the //BoxColider, then TargetShot = true.
var TargetGUI : GUIText; //Area where the text shows up
var Target : GameObject; //The target itself
var Targets : float = 6; //How many targets are in the scene
var AddCount : boolean = false; //Experimental
var TargetCounter : int = 1; //How many to had to the TargetsDown
var TargetsDown : float = 0; /How many targets have been shot
function Update () { //made to keep the GUI in one place, weird glitches in past.
TargetGUI.guiText.pixelOffset = Vector2 (Screen.width * -0.49, Screen.height - Screen.height / 0.78);
}
function OnTriggerEnter(other : Collider) { //If the tag = Bullet...
if(gameObject.GetComponent(BoxCollider).enabled == false) { //If the BoxCol. is deactivated, then add one point to TargetsDown
TargetsDown += TargetCounter;
}
if(other.tag == "Bullet") { //If the tag = "Bullet", then initiate animation
TargetShot = true;
}
if(TargetShot == true) { //Animation and BoxCollider deactivation
gameObject.animation.Play(TargetAnim);
AudioSource.PlayClipAtPoint(pickup, SoundLocation.transform.position, 8);
gameObject.GetComponent(BoxCollider).enabled = false;
}
}
function OnGUI() {
TargetGUI.text = "Targets Hit: " + TargetsDown + " / " + Targets.ToString();
TargetGUI.guiText.pixelOffset = Vector2 (Screen.width * -0.49, Screen.height - Screen.height / 0.78);
}
EDIT:
Also, when I step inside the collider of a target, it acts like I'M the "Bullet"... not sure what's wrong.
Answer by HarshadK · Apr 14, 2014 at 07:06 AM
In your OnTriggerEnter you are checking if the BoxCollider is false first (but it is still enabled) and then set it to be false in later if statement at the end.
Actually you should set your BoxCollider to be false first after it is hit and then check for it to increment the TargetsDown variable.
function OnTriggerEnter(other : Collider) { //If the tag = Bullet...
if(other.tag == "Bullet") { //If the tag = "Bullet", then initiate animation
TargetShot = true;
}
if(TargetShot == true) { //Animation and BoxCollider deactivation
gameObject.animation.Play(TargetAnim);
AudioSource.PlayClipAtPoint(pickup, SoundLocation.transform.position, 8);
gameObject.GetComponent(BoxCollider).enabled = false;
}
if(gameObject.GetComponent(BoxCollider).enabled == false) { //If the BoxCol. is deactivated, then add one point to TargetsDown
TargetsDown += TargetCounter;
}
}
The above script check if Game Object is hit then set TargetShot to true, and if TargetShot is true then play animation and set BoxCollider to false, then check if BoxCollider to be false and add TargetCounter to TargetsDown.
One thing though, if you are using integer values for the TargetsDown then you don't need to use flot for it. Whay don't you just use an 'int' instead of 'float'?