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
相关产品推荐
相关产品推荐

