You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity 2D横版游戏Android按钮控制器失效问题求助

2D横版游戏Android控制按钮失效排查(基于CrossPlatformInput)

我是刚接触Unity的游戏开发者,目前正在开发一款2D横版游戏,仅剩Android控制按钮功能未完成。我使用Unity标准资源(Standard Assets)中的CrossPlatformInput来实现所需的Android控制器,但测试后发现完全无法控制游戏角色移动。已多次严格按照教程操作,结果仍未改变,恳请帮忙排查问题。

以下是出现问题的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class Pemain : MonoBehaviour
{
    public Rigidbody2D rb;
    public float movespeed = 5f;
    public int jumppower;
    public Transform groundCheck;
    public float groundCheckRadius;
    public LayerMask whatIsGround;
    private bool onGround;
    private Animator anim;
    private int facing; 

    void Start ()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        jumppower = 5;
        facing = 1;
    }

    void FixedUpdate ()
    {
        onGround = Physics2D.OverlapCircle (groundCheck.position,groundCheckRadius,whatIsGround);
    }

    void Update () {

        float move = CrossPlatformInputManager.GetAxis("Horizontal");

        if (Input.GetKey(KeyCode.LeftArrow))
        {
            rb.velocity = new Vector2 (-movespeed, rb.velocity.y);
            anim.SetBool ("Walking", true);
            if (facing == 1)
            {
                transform.localScale = new Vector3 (-1f, 1f, 1f);
                facing = 0;
            }
        } else if (Input.GetKey(KeyCode.RightArrow))
        {
            rb.velocity = new Vector2(movespeed, rb.velocity.y);
            anim.SetBool ("Walking", true);
            if (facing == 0)
            {
                transform.localScale = new Vector3 (1f, 1f, 1f);
                facing = 1;
            }
        } else
        {
            anim.SetBool ("Walking", false);
        }
        if (Input.GetKey(KeyCode.Space) && onGround)
        {
            rb.velocity = new Vector2 (rb.velocity.x,jumppower);
        }
    }
}

问题核心排查

你的代码里完全没有使用CrossPlatformInput获取的move变量,所有移动、跳跃逻辑都只绑定了键盘的LeftArrow/RightArrow/Space按键,安卓触摸按钮的输入根本没被处理,这是导致安卓控制失效的直接原因。

修复方案

1. 重构输入逻辑,兼容CrossPlatformInput

把Update方法里的键盘判断替换为基于CrossPlatformInput的输入处理,这样同时支持键盘和安卓触摸操作:

void Update () {
    float move = CrossPlatformInputManager.GetAxis("Horizontal");

    // 处理移动逻辑
    if (move != 0)
    {
        rb.velocity = new Vector2(move * movespeed, rb.velocity.y);
        anim.SetBool("Walking", true);
        
        // 自动转向
        int targetFacing = move > 0 ? 1 : 0;
        if (facing != targetFacing)
        {
            transform.localScale = new Vector3(move > 0 ? 1f : -1f, 1f, 1f);
            facing = targetFacing;
        }
    }
    else
    {
        anim.SetBool("Walking", false);
    }

    // 替换跳跃输入为CrossPlatformInput的虚拟按钮
    if (CrossPlatformInputManager.GetButtonDown("Jump") && onGround)
    {
        rb.velocity = new Vector2(rb.velocity.x, jumppower);
    }
}

2. 检查CrossPlatformInput的虚拟控制配置

  • 确认项目中已正确导入Standard Assets/CrossPlatformInput包,无文件缺失
  • 在Unity编辑器中,确保已创建名为Horizontal的虚拟轴和Jump的虚拟按钮
  • 安卓端的UI控制按钮,要绑定对应的CrossPlatformInput事件:比如左按钮绑定Horizontal轴减操作,右按钮绑定Horizontal轴加操作,跳跃按钮绑定Jump按下事件

3. 额外排查点

  • 检查角色的Rigidbody2D组件是否启用,重力参数设置是否合理
  • 安卓打包时,确保Build Settings中已正确切换到安卓平台,输入权限正常开启

内容的提问来源于stack exchange,提问作者AryMans

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 00:50:16