WHAT I TRY? A script that notice when a button is pressed and while it's pressed it should make a object move to the left.
WHAT'S THE PROBLEM? No matter where I touch the screen, the Rididbody moves. But i want the body just moving when the Button is pressed.
I know there must be a posibility to make it work with INPUT.GetButton("ButtonName")
but i didnt get it working.
Thanks for help!
public class LeftTouch : MonoBehaviour {
public Rigidbody rb;
public float leftForce = 120;
bool pressed = false;
// Update is called once per frame
void Update () {
//Is there a touch?
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
//TOUCH START
pressed = true;
}
if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
//TOUCH END
pressed = false;
}
}
if (pressed == true)
{
//Make ridigbody move to left
rb.AddForce(-leftForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
}
}