简体   繁体   English

在Unity中滑动时如何移动角色?

[英]How can I make my character move when slide in Unity?

I would like make my character move to the left or to the right when slide the screen or touch a virtual button in a phone for Unity 3D, this is the player's script of the movement to move it with the keyboard keys, but I would like it to be for mobile. 我希望在滑动屏幕或触摸手机中的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();
    }

}

Most basic way to handle this would be (I think) something along the lines of: 解决此问题的最基本方法是(我认为):

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; 
    }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM