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

Unity Roll A Ball教程报错Vector2不含Y定义问题求助

Unity Roll-A-Ball 玩家移动功能编译错误解决

报错信息

跟随Unity Roll-A-Ball教程学习到第8步实现玩家移动功能,编写代码后抛出以下编译错误:

'Vector2 does not contain a definition for 'Y' and no accessible extension method 'Y' accepting a first argument type of 'Vector2' could be found. (are you missing a using directive or an assembly reference?)

错误原因

C#是大小写敏感的编程语言,Unity的Vector2结构体的坐标分量为小写开头的公共字段x、y,代码中误写为大写开头的X、Y,编译器无法找到对应成员,因此抛出编译错误。

修复方案

  • 找到OnMove方法中读取输入向量分量的两行代码,将大写的X、Y修改为小写的x、y即可。

修复后完整代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{

    private Rigidbody rb;
    private float movementX;
    private float movementY;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

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

        movementX = movementVector.x;
        movementY = movementVector.y;
    }

    void FixedUpdate()
    {
        Vector3 movement = new Vector3(movementX, 0.0f, movementY);
        rb.AddForce(movement);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 01:54:34