【问题标题】:Adding force to a rigidbody not working. Error: UnityEngine.Component does not contain a definition for AddForce and no extension method for AddForce向刚体添加力不起作用。错误:UnityEngine.Component 不包含 AddForce 的定义和 AddForce 的扩展方法
【发布时间】:2015-03-25 01:56:36
【问题描述】:

这是我的代码:

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {
    public float moveSpeed;
    public Vector3 input;


    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        input = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis("Vertical"));
        Rigidbody2D myRigidBody = rigidbody.AddForce (input * moveSpeed);

    }
}

我正在关注 [这里](https://www.youtube.com/watch?v=qwuPiaFU37w&list=PL0MyxI8F9YPGu6Nxjyv0smaaquNIPp4I-&index=1] 和 27:00 左右显示的教程,他说我不需要引用它,因为它内置在 Unity 中。我尝试添加引用但似乎无法让它工作。我试过看看我是否在视频中搞砸了任何带有名称或复选框等的东西,但似乎没有任何问题,我可能在哪里搞砸了,我该如何解决?

我使用的是 Windows 7 和 64 位 Unity。

【问题讨论】:

    标签: c# unity3d monodevelop


    【解决方案1】:

    您使用的是 Unity5 吗?

    Unity5 中的 API 发生了变化...例如要访问刚体组件,现在您必须使用 GetComponent<Rigidbody>()

    http://blogs.unity3d.com/2014/06/23/unity5-api-changes-automatic-script-updating/

    【讨论】:

    • Aaaaaah.... 他们为什么要这样做?!现在这么多教程不再起作用了。我降级到 Unity4,它现在可以工作了。谢谢编辑:如何将其标记为已回答? ...如果它被标记为已回答,请忽略它。
    • 此更改标准化了您访问任何组件的方式,因此是一种更好的方法(更不用说学习上的困惑了)。所以我建议习惯使用 GetComponent().
    猜你喜欢
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多