Touch button triggers once per press

Hello.

I’m having small problem here and i need little help. I’ll be happy if you guys can give me a hand on that…

What i need to do: It’s a platformer game, for mobile. I have 4 buttons (Run left / Right, Jump, Shoot).
What i want to be able to do is to multi touch the buttons and be able to slide my finger between them, so if the player moves it’s finger between running left and right the character shoud change it’s direction.

This is the initial script that is attached to a group that is a parent of all the buttons. (Buttons are GUITextures).

The script below works fine with all i need. The only problem is that i need to be able to press the jump button and it shoud execute the jumping only once, untill i release it and press it again. At the moment when i hold the jump button, the character is continously jumping.

I know that i can’t jump once per press, cuz the Release functions are executing every frame as well. But i can’t manage to make them execute only once…

//Public Variables
public var leftButton		: GUITexture;		// var so attach the GUI buttons to
public var rightButton		: GUITexture;
public var jumpButton		: GUITexture;
public var shootButton		: GUITexture;

private var jumpPressed		: boolean = false;    // To prevent continously jumping when jump bttn is hold down

function Update()
{
	ReadButtons();		// Call the function that register the buttons press / release
}


function ReadButtons()
{

	LeftButtonRelease();
	RightButtonRelease();
	JumpButtonRelease();
	ShootButtonRelease();

	var count : int = Input.touchCount;
	
	if (count > 0)
	{
		
		for (var i : int = 0; i < count; i++)
		{
			var touch : Touch = Input.GetTouch(i);
			
			if (leftButton.HitTest(touch.position))
			{
				LeftButtonPress();
				leftBttnBool = true;
			}
			
			if (rightButton.HitTest(touch.position))
			{
				RightButtonPress();
				rightBttnBool = true;
			}
				
			if (jumpButton.HitTest(touch.position))
			{
				JumpButtonPress();
				jumpBttnBool = true;
			}
				
			if (shootButton.HitTest(touch.position))
			{
				ShootButtonPress();
				shootBttnBool = true;
			}
		}
	}
}

function LeftButtonPress()
{
	leftButton.GetComponent(MoveButtonLeft_JS).OnTouchBegan();
}

function LeftButtonRelease()
{
	leftButton.GetComponent(MoveButtonLeft_JS).OnTouchEnd();
}

function RightButtonPress()
{
	rightButton.GetComponent(MoveButtonRight_JS).OnTouchBegan();
}

function RightButtonRelease()
{
	rightButton.GetComponent(MoveButtonRight_JS).OnTouchEnd();
}

function JumpButtonPress()
{
	jumpButton.GetComponent(JumpBttn_JS).OnTouchBegan();
}

function JumpButtonRelease()
{
	jumpButton.GetComponent(JumpBttn_JS).OnTouchEnd();
}

function ShootButtonPress()
{
	shootButton.GetComponent(ShootBttn_JS).OnTouchBegan();
}

function ShootButtonRelease()
{
	shootButton.GetComponent(ShootBttn_JS).OnTouchEnd();
}

Thanks :slight_smile:

I recall that the mouse events are emulated with touch events nowadays.
So if you need to do something on “sliding out”, you could use the OnMouseExit function. I think that’s triggered with touches also.

So what you’d want to do is:

  • Trigger ***ButtonPress on OnMouseDown & OnMouseEnter
  • Trigger ***ButtonRelease on OnMouseUp & OnMouseExit

(disclaimer: not 100% sure it works with multi-touch, but worth a try)