unity3d - Unity 脚本错误 CS1061“Vector2”不包含“GetAxis”的定义
问题描述
我在 Unity 中收到的完整错误消息,我正在使用 mac。
Assets/Scripts/Controller.cs(19,45):错误 CS1061:“Vector2”不包含“GetAxis”的定义,并且找不到接受“Vector2”类型的第一个参数的可访问扩展方法“GetAxis”(是您缺少 using 指令或程序集引用?)
这是我正在使用的代码:
using UnityEngine;
using System.Collections;
public class Controller : MonoBehaviour {
public float speed;
private Vector2 moveVelocity;
private Rigidbody2D rigidBody;
void Start() {
rigidBody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update() {
Vector2 moveInput = new Vector2(moveInput.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
moveVelocity = moveInput.normalized * speed;
}
void fixedUpdate () {
rigidBody.MovePosition(rigidBody.position + moveVelocity * Time.fixedDeltaTime);
}
}
如果需要任何其他信息,请告诉我。
解决方案
我想你有Input
困惑moveInput
。此外,如果您有 RigidBody 或 RigidBody2d,您应该尝试不要直接修改 GameObjects 的位置,而是使用该AddForce
方法。我会删除固定的更新方法,而是将您的更新方法修改为以下内容:
void Update() {
Vector2 moveInput = new Vector2(Input.GetAxis("Horizontal"),Input.GetAxis("Vertical"));
rigidBody.AddForce(moveInput.normalized * speed);
}
推荐阅读
- c3.js - 在 C3.js 中更改网格颜色
- javascript - CleaveJS phoneRegionCode 在 React 中不起作用
- javascript - 这是从数组创建列表的正确方法吗?
- html - 在子组件中向上滚动时会触发离子刷新器
- android - 使用 PowerMockito 测试 Android 原生代码
- c# - .net 核心应用程序在 docker 容器中的内存快照
- knex.js - 如何从 objection.js 中获取所有行
- javascript - 为制表器创建一个完全自定义的分页组件
- c# - 是否可以通过从 Startup.cs 中的 DbContext 获取值来设置 Cookie ExpireTimeSpan?
- react-native - 风格可能是假的,但不是真的 galio.io