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

Unity安卓滑动控制脚本逻辑正确但真机无法生效问题

问题排查与修复方案

核心问题1:角色轨道移动逻辑仅在编辑器生效

你把角色向左/中/右轨道移动的Vector3.Lerp代码完全放在了#if UNITY_EDITOR块内,安卓端根本不会执行这部分逻辑——哪怕触摸事件修改了current_pos,角色也不会移动到目标轨道位置,这是真机无法控制的主要原因。

核心问题2:未初始化dragDistance变量

安卓触摸逻辑里的dragDistance没有初始化,默认值为0,导致滑动检测条件Mathf.Abs(lp.x - fp.x) > dragDistance永远不成立,触摸操作无法触发角色动作。

其他潜在问题

  • 安卓端调用跳跃逻辑时,若Forward_jump()是协程,需用StartCoroutine(Forward_jump())启动,否则协程不会执行。
  • 未确认UNITY_ANDROID编译符号是否正确配置。

修复后的完整Update代码

private Vector2 fp, lp;
private float dragDistance;

void Start()
{
    // 初始化拖动阈值为屏幕高度的20%
    dragDistance = Screen.height * 0.2f;
}

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        isplay = true;
        player_animator.SetFloat("is_running", 1);
    }

    if (isplay)
    {
        transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + for_Speed * Time.deltaTime);

        // ===== 轨道移动逻辑移到条件编译外,让安卓和编辑器都能执行 =====
        if (current_pos == 0)
        {
            transform.position = Vector3.Lerp(transform.position, new Vector3(center_pos.position.x, transform.position.y, transform.position.z), side_speed * Time.deltaTime);
        }
        else if (current_pos == 1)
        {
            transform.position = Vector3.Lerp(transform.position, new Vector3(left_pos.position.x, transform.position.y, transform.position.z), side_speed * Time.deltaTime);
        }
        else if (current_pos == 2)
        {
            transform.position = Vector3.Lerp(transform.position, new Vector3(right_pos.position.x, transform.position.y, transform.position.z), side_speed * Time.deltaTime);
        }

#if UNITY_EDITOR
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            if (current_pos == 0)
            {
                current_pos = 1;
            }
            else if (current_pos == 2)
            {
                current_pos = 0;
            }
        }
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            if (current_pos == 0)
            {
                current_pos = 2;
            }
            else if (current_pos == 1)
            {
                current_pos = 0;
            }
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (onplatform)
            {
                rb.AddForce(Vector3.up * jump_force, ForceMode.Force);
                StartCoroutine(Forward_jump());
            }
        }

#elif UNITY_ANDROID
        if (Input.touchCount == 1) 
        {
            Touch touch = Input.GetTouch(0); 
            if (touch.phase == TouchPhase.Began) 
            {
                fp = touch.position;
                lp = touch.position;
            }
            else if (touch.phase == TouchPhase.Moved) 
            {
                lp = touch.position;
            }
            else if (touch.phase == TouchPhase.Ended) 
            {
                lp = touch.position;  

                if (Mathf.Abs(lp.x - fp.x) > dragDistance || Mathf.Abs(lp.y - fp.y) > dragDistance)
                {
                    if (Mathf.Abs(lp.x - fp.x) > Mathf.Abs(lp.y - fp.y))
                    {   
                        if ((lp.x > fp.x))  
                        {   
                            Debug.Log("Right Swipe");
                            if (current_pos == 0)
                            {
                                current_pos = 2;
                            }
                            else if (current_pos == 1)
                            {
                                current_pos = 0;
                            }
                        }
                        else
                        {   
                            Debug.Log("Left Swipe");
                            if (current_pos == 0)
                            {
                                current_pos = 1;
                            }
                            else if (current_pos == 2)
                            {
                                current_pos = 0;
                            }
                        }
                    }
                    else
                    {   
                        if (lp.y > fp.y)  
                        {   
                            Debug.Log("Up Swipe");
                            if (onplatform)
                            {
                                rb.AddForce(Vector3.up * jump_force, ForceMode.Force);
                                StartCoroutine(Forward_jump());
                            }
                        }
                        else
                        {   
                            Debug.Log("Down Swipe");
                        }
                    }
                }
                else
                {   
                    Debug.Log("Tap");
                }
            }
        }
#endif
    }
}

额外验证步骤

  1. 确认打包平台为Android:在Unity顶部菜单栏选择File > Build Settings,勾选Android并点击Switch Platform。
  2. 查看安卓日志:使用Unity的Window > Analysis > Logcat工具,连接真机后查看Debug输出,确认触摸事件的日志是否正常打印,判断代码是否执行。

内容的提问来源于stack exchange,提问作者yiğit kıy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 12:44:50