- Home /
Problem with touch button (when moved)
I got this script for android intput it works fine but the problem comes when the player holds the button and then slides the finger out of it; the button stays pressed even tho the finger is not over it, it stays pressed even when the user doesnt have a finger over the screen(after the sliding thing). How may I solve this issue? Any idea?
public Texture2D pressed, notPressed; public Rigidbody2D Player;
void Update(){
this.guiTexture.pixelInset = new Rect(0, 0, Screen.width/3.5f, Screen.height/6f);
SpeedController playerScript = Player.GetComponent<SpeedController>();
if (Input.touches.Length <= 0) {
playerScript.move = 0;
} else
{
for (int i= 0; i < Input.touchCount; i++) {
if (this.guiTexture.HitTest(Input.GetTouch(i).position))
{
if(Input.GetTouch(i).phase == TouchPhase.Began){
playerScript.left = true;
this.guiTexture.texture = pressed;
}
if(Input.GetTouch(i).phase == TouchPhase.Ended){
playerScript.left = false;
this.guiTexture.texture = notPressed;
}
}
}
}
}
}
Answer by DGKN · Jul 24, 2014 at 08:34 PM
Try this :
for (int i= 0; i < Input.touchCount; i++)
{
if (this.guiTexture.HitTest(Input.GetTouch(i).position))
{
if(Input.GetTouch(i).phase == TouchPhase.Began)
{
playerScript.left = true;
this.guiTexture.texture = pressed;
}
if(Input.GetTouch(i).phase == TouchPhase.Ended)
{
playerScript.left = false;
this.guiTexture.texture = notPressed;
}
}
else
playerScript.left = false;
}
If you hold down the button and release it outside the HitTest area, then playerScript.left remains always TRUE because
if (this.guiTexture.HitTest(Input.GetTouch(i).position))
doesn't apply anymore. So you need a ... = false statement to stop the movement once you're out of the Touch area.
I already did that :P but that wasnt the solution because if I do it that way I cannot press two different buttons at the same time(because if I press the second one, the bool gets false) i have donne it in away that doesnt affect the other buttons but if I touch with another finger any part of the screen that is not a GUItexture it goes false and the player stops moving, my solution is just as yours but with an "if" statement, lets see if someone else or you have a better one :)
Answer by dadika4 · Jun 28, 2017 at 11:31 AM
using UnityEngine;
public class TouchInput : MonoBehaviour {
Ray ray;
RaycastHit hit;
Rigidbody rb;
public bool left;
public bool right;
public bool jump;
public int speed;
void Start()
{
rb = GameObject.FindGameObjectWithTag("player").GetComponent<Rigidbody>();
speed = 1;
}
void Update()
{
if (left)
{
rb.AddForce(-10*speed, 0, 0);
}
if (right)
{
rb.AddForce(10*speed, 0, 0);
}
if (jump)
{
rb.AddForce(0, 10*speed, 0);
}
Touch[] myTouches = Input.touches;
if (Input.touchCount > 0)
{
for (int i = 0; i < Input.touchCount; i++)
{
ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
if (Physics.Raycast(ray, out hit, Mathf.Infinity) == true)
{
//touch phase has begun
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
if (hit.collider.tag == "left")
{
left = true;
}
else if (hit.collider.tag == "right")
{
right = true;
}
else if (hit.collider.tag == "jump")
{
jump = true;
}
}
//touch phase has ended for buttons
else if (Input.GetTouch(i).phase == TouchPhase.Ended)
{
if (hit.collider.tag == "left")
{
left = false;
}
else if (hit.collider.tag == "right")
{
right = false;
}
else if (hit.collider.tag == "jump")
{
jump = false;
}
}
}
if (Physics.Raycast(ray, out hit, Mathf.Infinity) == false)
{
left = false;
right = false;
jump = false;
}
}
}
}
}
Jup the script lacked the physics ending touch on the sprite.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
how use specific for mobile button when we have 3 ? 1 Answer
GUI Button touch problem 1 Answer
Taking Input from Android phones 1 Answer
Touch Button Android 0 Answers