javascript - 在 Unity 上运行 Javascript 代码出现错误。

标签 javascript unity-game-engine game-engine

大家好,尝试构建一个简单的游戏,

这是我的代码 当我运行此代码时,它给我错误“playerMovement”不是“UnityEngine.Rigidbody”的成员。

public var speed : int = 15;
var player : Rigidbody;
var player2 : Rigidbody;

function Start () {
player = GetComponent.<Rigidbody>();
player2 = GetComponent.<Rigidbody>();
}

function playerMovement(){
if(Input.GetKey(KeyCode.UpArrow)){
    player.AddForce(Vector3.forward*speed);
    Debug.Log("Player 1 is moving forward");
}
if(Input.GetKey(KeyCode.DownArrow)){
    player.AddForce(Vector3.back*speed);
}
if(Input.GetKey(KeyCode.LeftArrow)){
    player.AddForce(Vector3.left*speed);
}
if(Input.GetKey(KeyCode.RightArrow)){
    player.AddForce(Vector3.right*speed);
}

}

function player2Movement(){
if(Input.GetKey(KeyCode.UpArrow)){
    player2.AddForce(Vector3.forward*speed);
    Debug.Log("Player 2 is moving forward");
}
if(Input.GetKey(KeyCode.DownArrow)){
    player2.AddForce(Vector3.back*speed);
}
if(Input.GetKey(KeyCode.LeftArrow)){
    player2.AddForce(Vector3.left*speed);
}
if(Input.GetKey(KeyCode.RightArrow)){
    player2.AddForce(Vector3.right*speed);
}

} 

function Update () {
player.playerMovement();
player2.playerMovement();
}

它给我错误“playerMovement”不是“UnityEngine.Rigidbody”的成员。 如何解决这个问题。

最佳答案

C# 移植代码时需要注意到 JavaScript。

player.playerMovement();
player2.playerMovement();

您调用的函数都在同一个脚本中。

直接调用他们即可。

playerMovement();
player2Movement();

另请注意上面的代码中player2Movement 中错误的拼写更改。

即使您修复了该问题,它也可以正常编译,但不会按预期工作,因为:

player = GetComponent.<Rigidbody>();
player2 = GetComponent.<Rigidbody>();

因为你有不同的游戏对象/球要移动。在获取每个对象的 Rigidbody 引用之前,您必须使用 GameObject.Find 找到它们。

player = GameObject.Find("Ball1").GetComponent.<Rigidbody>();
player2 =  GameObject.Find("Ball2").GetComponent.<Rigidbody>();

最后,控件被搞乱了。您正在使用相同的控件。我认为这是一个糟糕的复制和粘贴或其他东西,但下面是您的整个工作代码。

注意:

在你说它不起作用之前,请确保你的两个游戏对象/球被命名为Ball1Ball2,并确保旧的C#冷是不依附于他们。代码已经过测试并且可以工作!

#pragma strict

public var speed : int = 15;
var player : Rigidbody;
var player2 : Rigidbody;

function Start () {
    player = GameObject.Find("Ball1").GetComponent.<Rigidbody>();
    player2 =  GameObject.Find("Ball2").GetComponent.<Rigidbody>();
}

function playerMovement(){
    if (Input.GetKey(KeyCode.A))
    {
        player.AddForce(Vector3.left * speed);
    }

    if (Input.GetKey(KeyCode.D))
    {
        player.AddForce(Vector3.right * speed);
    }

    if (Input.GetKey(KeyCode.W))
    {
        player.AddForce(Vector3.forward * speed);
        Debug.Log("Player 1 is moving forward");
    }
    if (Input.GetKey(KeyCode.S))
    {
        player.AddForce(Vector3.back * speed);
    }
}

function player2Movement(){
    if(Input.GetKey(KeyCode.UpArrow)){
        player2.AddForce(Vector3.forward*speed);
        Debug.Log("Player 2 is moving forward");
    }
    if(Input.GetKey(KeyCode.DownArrow)){
        player2.AddForce(Vector3.back*speed);
    }
    if(Input.GetKey(KeyCode.LeftArrow)){
        player2.AddForce(Vector3.left*speed);
    }
    if(Input.GetKey(KeyCode.RightArrow)){
        player2.AddForce(Vector3.right*speed);
    }

} 

function Update () {
    playerMovement();
    player2Movement();
}

关于javascript - 在 Unity 上运行 Javascript 代码出现错误。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37586639/

相关文章:

javascript - 检索 Json 字符串的一部分

javascript - 打开下拉菜单时如何更改 Material UI Select 组件(在轮廓模式下)的轮廓颜色(当前为蓝色)

c++ - 如果我使用 malloc() 而不是堆栈数组,OpenGL 不会渲染对象?

unity-game-engine - 在unity3d中预加载纹理

c# - 使用游戏引擎 (Unity3D) 进行多线程?

javascript - HTML5 - 如何绘制完成的 Canvas /图像

javascript - Canvas 绘制图像缩放

javascript - Bokeh:用于调整 x 轴范围的 CustomJS TextInput 回调

c# - 在 Unity 中通过音频源播放 NAudio .mp3

python - 如何通过套接字从 python 向 Unity 发送和接收 numpy 数组