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

Unity滚球教程中OnMove函数未触发,玩家无法移动求助

Unity滚球教程中OnMove函数未触发导致无法移动的解决方法

你的代码逻辑本身没问题,OnMove函数未被调用的核心原因是新输入系统的绑定设置未完成,以下是具体排查和修复步骤:

  • 检查PlayerInput组件配置
    确保玩家对象上添加了PlayerInput组件,并且:

    1. 在Actions字段选择对应的Input Action Asset(无则右键菜单选Input Actions新建);
    2. Behavior选项选择Send Messages或Invoke Unity Events(选Send Messages会自动调用脚本中匹配的OnXXX方法,比如OnMove);
    3. 若选Invoke Unity Events,需手动在Events面板将Move动作的触发事件绑定到playerController脚本的OnMove方法。
  • 验证Input Action的绑定
    打开你的Input Action Asset,找到Move动作:

    1. 确保Move动作的Type为Value,控制类型是Vector2;
    2. 给Move动作添加绑定(比如WASD键或方向键),测试输入是否能触发动作。
  • 检查脚本与文件名一致性
    你的脚本类名是playerController(小写开头),必须确保脚本文件名也是playerController.cs——Unity要求脚本文件名和类名严格一致,否则组件无法正确识别方法。

  • 确认输入系统启用状态
    打开Edit > Project Settings > Player,在Active Input Handling选项中选择Both或New Input System,若选Old Input System,新输入系统的方法不会被触发。

附你的代码(供参考):

using UnityEngine;
using UnityEngine.InputSystem;

public class playerController : MonoBehaviour
{
    private Rigidbody rb;
    private float moveX;
    private float moveY;

    void Start()
    {
        rb = GetComponent<Rigidbody>(); //ref to player's rigidbody
    }

    void OnMove(InputValue movementValue)
    {
        Vector2 movementVector = movementValue.Get<Vector2>();

        moveX = movementVector.x;
        moveY = movementVector.y;
    }

    private void FixedUpdate()
    {
        Vector3 movement = new Vector3(moveX, 0.0f, moveY);
        rb.AddForce(movement);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 12:55:04