当我滑动屏幕或触摸Unity 3D手机中的虚拟按钮时,我想让角色向左或向右移动,这是玩家使用键盘按键移动角色的动作脚本,但是我希望它可以用于移动设备。
void FixedUpdate ()
{
// Add a forward force
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d")) // If the player is pressing the "d" key
{
// Add a force to the right
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a")) // If the player is pressing the "a" key
{
// Add a force to the left
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (rb.position.y < -1f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
答案 0 :(得分:0)
(我认为)处理此问题的最基本方法是:
float lastPosition;
void OnSwipte(float delta)
{
// code your movement here
}
void Update()
{
if (Input.GetMouseButtonDown(0))
lastPosition=Input.mousePosition.x; // store touch point
else
if (Input.GetMouseButton(0))
{
OnSwipe(Input.mousePosition.x-lastPosition);
lastPosition=Input.mousePosition.x;
}
}