Unity javascript error Rigidbody

Unity javascript error Rigidbody

本文关键字:Rigidbody error javascript Unity      更新时间:2023-09-26
 #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);
}

如果能给我一个带代码的答案,我将不胜感激。谢谢你花时间回复。

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

private Rigidbody rb;rb = GetComponent<Rigidbody>();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);
}