首页 > 解决方案 > 如何使用更改以下代码在使用触摸控件的设备上工作?

问题描述

我正在使用 Unity 和 C# 创建游戏。下面的代码使玩家可以使用计算机上的箭头键左右移动,但我需要它在 Andriod 设备上工作。有没有一种简单的方法可以使用此代码通过使用触摸控件而不重写所有代码来使游戏在 Andriod 设备上运行?

void Start()
{
    myRigidbody = GetComponent<Rigidbody2D>();
    myAnim = GetComponent<Animator>();
}

// Update is called once per frame
void Update()
{
    isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);

    if (Input.GetAxisRaw("Horizontal") > 0f)
    {
        myRigidbody.velocity = new Vector3(moveSpeed, 
        myRigidbody.velocity.y, 0);
    }
    else if (Input.GetAxisRaw("Horizontal") < 0f)
    {
        myRigidbody.velocity = new Vector3(-moveSpeed, 
        myRigidbody.velocity.y, 0f);
    }
    else
    {
        myRigidbody.velocity = new Vector3(0f, myRigidbody.velocity.y,0f);
    }

    if (Input.GetButtonDown("Jump") && isGrounded)
    {
        myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, jumpSpeed, 0f);
    }

    myAnim.SetFloat("Speed", Mathf.Abs(myRigidbody.velocity.x));
    myAnim.SetBool("Grounded", isGrounded);
    }
}

标签: androidunity3d

解决方案


尝试这个。三个变量(leftBtn、rightBtn 和 jump)将被相应地触发:

  1. 如果触摸屏幕的右下角,rightBtn = true
  2. 如果触摸屏幕左下角,leftBtn = true
  3. 如果触摸屏幕上半部分的任意位置,则跳转 = true

-

bool leftBtn = false;
bool rightBtn = false;
bool jump = false;

void Update()
{
    leftBtn = false;
    rightBtn = false;
    jump = false;
    if(Input.touches.Length > 0){
        Touch touch = Input.touches [0];
        if (touch.position.y > Screen.height / 2) {
            jump = true;
        } else {
            if (touch.position.x > Screen.width / 2) {
                rightBtn = true;
            } else {
                leftBtn = true;
            }
        }
    }

    //put the rest Of your code here \/
}

因此,我认为这将是您的代码中的样子(但我可能走错路了):

bool leftBtn = false;
bool rightBtn = false;
bool jump = false;

void Start()
{
    myRigidbody = GetComponent<Rigidbody2D>();
    myAnim = GetComponent<Animator>();
}

// Update is called once per frame
void Update()
{
    leftBtn = false;
    rightBtn = false;
    jump = false;
    if(Input.touches.Length > 0){
        Touch touch = Input.touches [0];
        if (touch.position.y > Screen.height / 2) {
            jump = true;
        } else {
            if (touch.position.x > Screen.width / 2) {
                rightBtn = true;
            } else {
                leftBtn = true;
            }
        }
    }


    isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);

    if (rightBtn)
    {
        myRigidbody.velocity = new Vector3(moveSpeed, 
            myRigidbody.velocity.y, 0);
    }
    else if (leftBtn)
    {
        myRigidbody.velocity = new Vector3(-moveSpeed, 
        myRigidbody.velocity.y, 0f);
    }
    else
    {
        myRigidbody.velocity = new Vector3(0f, myRigidbody.velocity.y,0f);
    }

    if (jump && isGrounded)
    {
        myRigidbody.velocity = new Vector3(myRigidbody.velocity.x, jumpSpeed, 0f);
    }

    myAnim.SetFloat("Speed", Mathf.Abs(myRigidbody.velocity.x));
    myAnim.SetBool("Grounded", isGrounded);
}

如果您愿意,可以使用 Screen.width / 2 和 Screen.height / 2 来获得屏幕的不同区域触发不同的功能。


推荐阅读