Touches Repeating?

So I have this code, but for some reason if you hold a button then it continues to make the effect happen over and over again. I thought touch.phase began only called once when it all began? Is there something wrong in the code?

//GUITextures
var mB : GUITexture;
var rB : GUITexture;
var sB : GUITexture;
var drB : GUITexture;
//var pB : GUITexture;

//ButtonHits
var MBHit;
var RBHit;
var SBHit;
var DRBHit;
//var PBHit;

//Transforms
var upperBody : Transform;
var lowerBody : Transform;
var shoulderR : Transform;
var shoulderL : Transform;

//Scripts
var AttackScript : AttackScript;

//Misc.
var aOne;
var aTwo;
var aThree;
var Aspeed : float;

//CharacterContoller
var character : CharacterController;

//Booleans
var roll : boolean = false;
var walking : boolean = false;
var attack : boolean = true;
var alive : boolean = true;
var grab : boolean = true;
var wSwitch : boolean = true;

//Ints
var wType : int = 1;
var tap : int;

//Floats
var ammoLeft : float = 40;
var clipSize : float = 5;
var ammoinClip : float = 5;
var mWep : float;
var rWep : float;
var Atime : float = 2;
var twoSpeed : float = 0.005;
var LastPress : float;
var timer : float;
var Ctime : float = 1;
var crouchHeight : float = 0.5;
var rollTime : float;
var diveSpeed : float = 1;
var time : float;
var rollSpeed : float = 2.0;
var idleSpeed : float = 0.5;
var health : float = 500.0;
var walkSpeed : float = 1.0;
var speed : float = 5.0;
var tTime : float = 0.5;
var switchTime : float = 0.5;

//GameObject Arrays
var mWeapons : GameObject[];
var rWeapons : GameObject[];
var bullets : GameObject[];

//Animation Arrays
var rGrabs : String[];
var mGrabs : String[];
var mAttack1s : String[];
var mAttack2s : String[];
var mAttack3s : String[];

function Start (){
//Starting Animation
AttackScript = mWeapons[mWep].GetComponent("AttackScript");
animation.Play(mGrabs[mWep]);
AttackSpeed();

//Set Weapons Inactive
for(var b = 0; b < rWeapons.length; b++){
rWeapons[b].SetActiveRecursively(false);
}
for(var a = 0; a < mWeapons.length; a++){
mWeapons[a].SetActiveRecursively(false);
}
if(wType == 1){
mWeapons[0].SetActiveRecursively(true);
}
else if(wType == 2){
rWeapons[0].SetActiveRecursively(true);
}

//Animation Time
rollTime = animation["Roll"].length / 2.5;

//Animation Multi (Allows animations to run simultaneously)
animation["GrabGun"].AddMixingTransform(upperBody);
animation["Walk"].AddMixingTransform(lowerBody);
animation["SSL"].AddMixingTransform(lowerBody);
animation["SSR"].AddMixingTransform(lowerBody);
animation["TKGun"].AddMixingTransform(shoulderR);
animation["SideGunR"].AddMixingTransform(shoulderR);
animation["SideGunL"].AddMixingTransform(shoulderL);
animation["IdleUp"].AddMixingTransform(upperBody);
animation["IdleLow"].AddMixingTransform(lowerBody);

//Animation Layers
animation["IdleLow"].layer = 1;
animation["IdleUp"].layer = 1;
animation["Roll"].layer = 3;
animation["SideGunR"].layer = 0;
animation["SideGunL"].layer = 0;
animation["SSR"].layer = 1;
animation["SSL"].layer = 1;
animation["TKGun"].layer = 0;
animation["GrabGun"].layer = 0;
animation["Walk"].layer = 1;

//Animation Speeds
animation["Roll"].speed = rollSpeed;
}

function Update () {
for (var i = 0; i < iPhoneInput.touchCount; i++){
var touch : iPhoneTouch = iPhoneInput.touches[i];
if (touch.phase == iPhoneTouchPhase.Began) {
MBHit = mB.HitTest(touch.position);
RBHit = rB.HitTest(touch.position);
SBHit = sB.HitTest(touch.position);
DRBHit = drB.HitTest(touch.position);
//PBHit = pB.HitTest(touch.position);
}
}

if(!roll){
if(walking){
if(DRBHit){
animation.CrossFade("Roll");
grab = true;
RollTime();
}
}
}

if(roll){
transform.Translate(Vector3.forward * diveSpeed);
}

if(RBHit){
if(wType == 1){
wType = 2;
DeactivateWeapons();
WeaponSwitch();
}
Fire();
}

if(MBHit){
if(wType == 2){
wType = 1;
DeactivateWeapons();
WeaponSwitch();
}
timer = Time.timeSinceLevelLoad;
if(attack){
if(MBHit){
LastPress = Time.timeSinceLevelLoad;
if(tap > 1){
tap = 1;
}
else{
tap++;
}
Combo();
}
}
if(timer - LastPress > tTime){
tap = 0;
if(timer - LastPress < Atime){
attack = false;
mWeapons[mWep].collider.active = true;
}
else{
attack = true;
mWeapons[mWep].collider.active = false;
}
}
}

if(health <= 0){
alive = false;
}
else{
alive = true;
}

if(alive){
if(wSwitch){
if(SBHit){
wSwitch = false;
SwitchButton();
}
}
}

if(Input.GetKeyDown("f")){
if(grab){
grab = false;
animation[rGrabs[rWep]].speed = speed;
animation[rGrabs[rWep]].time = 0.0;
animation.Play(rGrabs[rWep]);
}
else if(!grab){
grab = true;
animation[rGrabs[rWep]].speed = -1 * speed;
animation[rGrabs[rWep]].time = animation[rGrabs[rWep]].length;
animation.Play(rGrabs[rWep]);
}
}
}

if(walking){
diveSpeed = twoSpeed;
}
else{
diveSpeed = 0.01;
}

if(Input.GetKey("w")){
animation["Walk"].speed = walkSpeed;
animation.CrossFade("Walk");
walking = true;
}
else if(Input.GetKey("s")){
animation["Walk"].speed = walkSpeed * -1;
animation.CrossFade("Walk");
walking = true;
}
else if(Input.GetKey("a")){
animation.CrossFade("SSL");
}
else if(Input.GetKey("d")){
animation.CrossFade("SSR");
}
else{
animation["IdleLow"].speed = idleSpeed;
animation.CrossFade("IdleLow");
walking = false;
}

function RollTime(){
roll = true;
if(wType == 1){
mWeapons[mWep].SetActiveRecursively(false);
}
if(wType == 2){
rWeapons[rWep].SetActiveRecursively(false);
}
yield WaitForSeconds(rollTime + 0.4);
roll = false;
if(wType == 1){
mWeapons[mWep].SetActiveRecursively(true);
}
if(wType == 2){
rWeapons[rWep].SetActiveRecursively(true);
}
}

function Combo () {
//WaitForSeconds(2);
if(attack){
switch (tap){

case 0:
//Reset to Idle
animation.CrossFadeQueued("Idle", 0.3, QueueMode.CompleteOthers);
break;

case 1:
//Combo Start
Atime = animation[mAttack3s[mWep]].length;
aOne = animation.PlayQueued(mAttack1s[mWep], QueueMode.PlayNow);
aOne.speed = Aspeed;
break;

case 2:
//Combo Prolonged
attack = false;
Atime += animation[mAttack2s[mWep]].length;
aTwo = animation.PlayQueued(mAttack2s[mWep], QueueMode.CompleteOthers);
aTwo.speed = Aspeed;
break;

case 3:
//Combo Finished
attack = false;
Atime += animation[mAttack3s[mWep]].length;
aThree = animation.PlayQueued(mAttack3s[mWep], QueueMode.CompleteOthers);
aThree.speed = Aspeed;
break;
}
}
}

function DeactivateWeapons(){
if(wType == 2){
mWeapons[mWep].SetActiveRecursively(false);
}
if(wType == 1){
rWeapons[rWep].SetActiveRecursively(false);
}
}

function WeaponSwitch(){
if(wType == 1){
for(var a = 0; a < mWeapons.length; a++){
mWeapons[a].SetActiveRecursively(false);
}
mWeapons[mWep].SetActiveRecursively(true);
AttackScript = mWeapons[mWep].GetComponent("AttackScript");
animation.Play(mGrabs[mWep]);
AttackSpeed();
}
else if(wType == 2){
for(var b = 0; b < rWeapons.length; b++){
rWeapons[b ].SetActiveRecursively(false);
}
rWeapons[rWep].SetActiveRecursively(true);
animation.Play(rGrabs[rWep]);
}
}

function Fire(){
rWeapons[rWep].animation.Play("Fire");
ammoinClip -= 1;
}

function SwitchButton(){
if(wType == 1){
DeactivateWeapons();
if(mWep >= mWeapons.length - 1){
mWep = 0;
WeaponSwitch();
yield WaitForSeconds(switchTime);
wSwitch = true;
}
else{
mWep++;
WeaponSwitch();
yield WaitForSeconds(switchTime);
wSwitch = true;
}
}
else if(wType == 2){
DeactivateWeapons();
if(rWep >= rWeapons.length - 1){
rWep = 0;
WeaponSwitch();
yield WaitForSeconds(switchTime);
wSwitch = true;
}
else{
rWep++;
WeaponSwitch();
yield WaitForSeconds(switchTime);
wSwitch = true;
}
}
}

function AttackSpeed(){
Aspeed = AttackScript.wSpeed;
}

function Reload(){
if(ammoinClip < clipSize){
rWeapons[rWep].animation.Play("Reload");
ammoAdd = clipSize - ammoinClip;
ammoinClip += ammoAdd;
ammoLeft -= ammoAdd;
}
}

function LoseHealth(damage){
health -= damage;
}

Hey Astrauk,
I haven’t had a chance to look over the rest of your code but I stripped it down to this, and it looks like the touch phases are working okay so it must be something else,
Run this script and see how your touch phases go -

function Update () {
	for (var i = 0; i < iPhoneInput.touchCount; i++){
		var touch : iPhoneTouch = iPhoneInput.touches[i];
		if (touch.phase == iPhoneTouchPhase.Began) {
			print ("Began");
		}
		if (touch.phase == iPhoneTouchPhase.Moved) {
			print ("Moved");
		}
		if (touch.phase == iPhoneTouchPhase.Ended) {
			print ("Ended");
		}				
	}
}

I’ll try that and post back here with the results. Thanks.

Dang sorry bout not replying back totally forgot, I tried your code and all the things are registering right, began is called once when the finger touches the screen, moved is continually called while the finger is moving across the screen, and ended is called once when the finger leaves the screen, so either its an issue with hit.test or my code.

Ran a debug script to check the hittest and it ran perfect, added those debug.log’s to my player control script and sure enough they are getting called over and over even if you just tap a button, it will keep getting called to you tap on another button.

So, sometimes it doesn’t get to ended/canceled? That seems to be an issue that pops up a bit. I had that a few times but I put in a little check that forces everything to end if the touchcount is zero. Kinda hacky but it worked for what I was doing.

Yup thats pretty much it, but I also have joysticks in use so this doesn’t seem like the best option, I’m going to rewrite my code (as it needs it anyway) and while doing that I’ll add bit by bit and see if I can find the source of the problem and fix it. Thanks a lot for the help.

No worries, good luck with it!

Ok I got it working by putting the hittests in the “if(touch.phase == iPhoneTouchPhase.Began)”. They now call right :slight_smile: