我在尝试什么? 一个脚本,当按下按钮时按下它并按下它时它应该使一个物体向左移动。
问题是什么? 无论我在哪里触摸屏幕,Rididbody都会移动。但是我希望按下按钮时身体才会移动。
我知道必须有可能使它与INPUT.GetButton("ButtonName")
一起工作,但我没有让它发挥作用。
谢谢你的帮助!
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);
}
}
}