Touch and hold on button error

Hi guys,

I’ve got a button which causes my character to swing his sword. The swing animation is set to ClampForever. I’d like it so that if the user holds their finger on the swing button after the animation is done, the tap is effectively counted as ended. The character goes back to idle and the player must lift their finger and tap again to have the player swing again.

Unfortunately right now, if the player taps and holds, the animation sits at the last frame (clamped forever) until the finger is lifted. The same happens if the player “spams” the button.

How can I make it so that the character returns to the idle position once the animation is over, even if the button is still being pressed? My butchered Penelope AnimationController script below:

var animationTarget : Animation; 
var maxForwardSpeed : float = 6; 
var maxBackwardSpeed : float = 3; 
var maxSidestepSpeed: float = 4; 
private var jumping : boolean = false; 
private var minUpwardSpeed = 2; 
private var character : CharacterController; 
private var thisTransform : Transform; 

var jumpButton : GUITexture; // change name from jumpButton to attackButton at some point 


function Start() 
{ 
   character = GetComponent(CharacterController); 
   thisTransform = GetComponent(Transform); 
    
   animationTarget.wrapMode = WrapMode.Loop; 
   animationTarget["Attack_part1"].wrapMode = WrapMode.ClampForever; 
} 

function Update() 
{ 
	if (iPhoneInput.touchCount > 0) 
	{
   		var touch : iPhoneTouch = iPhoneInput.GetTouch(0); 
    
  	 	if (jumpButton.HitTest(touch.position))          
      	{ 
        	 animationTarget["Attack_part1"].speed = 1.6; 
         	animationTarget.Play("Attack_part1"); 
      	} 
	}
       
    
   var characterVelocity = character.velocity; 
   var horizontalVelocity : Vector3 = characterVelocity; 
    
   horizontalVelocity.y = 0; 
    
   var speed = horizontalVelocity.magnitude; 
   /* 
   var upwardsMotion = Vector3.Dot(thisTransform.up, characterVelocity); 
   if (!character.isGrounded  upwardsMotion > minUpwardSpeed) 
   { 
      jumping = true; 
   } 
   */ 
   if (animationTarget.IsPlaying("bounce_walk")  
   animationTarget["bounce_walk"].normalizedTime < 1.0  
   speed > 0) 
   { 
      // 
   } 
   else if (animationTarget.IsPlaying("Attack_part1")  
   animationTarget["Attack_part1"].normalizedTime < 0.5  
   speed == 0) 
   { 
      // 
   } 
   else if (speed > 0) 
   { 
      var forwardMotion = Vector3.Dot(thisTransform.forward, horizontalVelocity); 
      var sidewaysMotion = Vector3.Dot(thisTransform.right, horizontalVelocity); 
      var t = 0.0; 
       
      if (forwardMotion > 0) 
      { 
         /* 
         t = Mathf.Clamp(Mathf.Abs(speed / maxForwardSpeed),0, maxForwardSpeed); 
         animationTarget["bounce_walk"].speed = Mathf.Lerp(0.25,1,t); 
         */ 
          
         if (animationTarget.IsPlaying("idle")) 
         { 
            animationTarget.Play("bounce_walk"); 
         } 
         else 
         { 
            animationTarget.CrossFade("bounce_walk"); 
         } 
      } 
   } 
   else 
   { 
      animationTarget.CrossFade("idle"); 
   } 
} 

function OnEndGame() 
{ 
   this.enabled = false; 
}

Can anyone help? Not looking for someone to write my code for me, simply looking for some direction on what I am sure is a very simple solution that is eluding me… :slight_smile:

You need to make use of the phase variable in the iPhoneTouch object. This tells you which stage the touch is at on the current frame (just begun, held down, moved, lifted). If you activate the animation only on the frame where the touch begins, it won’t keep playing when the “button” is held down.