【问题标题】:Unity javascript error RigidbodyUnity javascript错误刚体
【发布时间】:2016-11-28 01:05:27
【问题描述】:
 #pragma strict

 private Rigidbody rb;

function Start () {
    rb = GetComponent<Rigidbody>();
}

function FixedUpdate () {
    var v : float = Input.GetAxis("Vertical");
    var h : float = Input.GetAxis("Horizontal");

    Vector3 movement = new Vector3 (h, 0.0f, v);
    rb.AddForce (movement);
}

我会很感激有一些代码给我看的答案。感谢您抽出宝贵时间回复。

【问题讨论】:

  • 那么,错误是什么?
  • Assets/Scripts/PlayerController.js(3,10): BCE0043: Unexpected token: Rigidbody.
  • 哦。还有这个 Assets/Scripts/PlayerController.js(13,16): UCE0001: ';'预期的。在末尾插入一个分号。可以清楚的看到v)后面的分号
  • 好的。检查我的答案。如果你想在 Unity 中编程,请学习 C#。

标签: javascript unity3d unityscript


【解决方案1】:

您在代码中混合了 Javascript 和 C#。它们是两种不同的语言。以下是问题:

private Rigidbody rb;、rb = GetComponent&lt;Rigidbody&gt;(); 和 Vector3 movement = new Vector3 (h, 0.0f, v);。

我建议您立即切换到 C#,因为 Unity 建议您这样做。大多数最新的文档都没有 Javascript 的示例,而且看起来 Javascript 支持将在未来移除。

我已将错误代码注释掉,以便您从错误中吸取教训。这是您的固定代码:

#pragma strict

//private Rigidbody rb;
private var rb:Rigidbody;

function Start () {
    //rb = GetComponent<Rigidbody>();
    rb = GetComponent.<Rigidbody>();
}

function FixedUpdate () {
    var v : float = Input.GetAxis("Vertical");
    var h : float = Input.GetAxis("Horizontal");

    //Vector3 movement = new Vector3 (h, 0.0f, v);
    var movement = Vector3(h, 0.0f, v);
    rb.AddForce (movement);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多