简体   繁体   English

在Unity上运行Javascript代码给出错误。

[英]Running Javascript code on Unity giving error.

Hello everyone trying to built a simple game, 大家好,试图建立一个简单的游戏,

this is my code when i run this code its giving me the error 'playerMovement' is not a member of 'UnityEngine.Rigidbody'. 这是我的代码,当我运行此代码时,它给我错误'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();
}

its giving me the error 'playerMovement' is not a member of 'UnityEngine.Rigidbody'. 它给我错误'playerMovement'不是'UnityEngine.Rigidbody'的成员。 how to resolve this. 如何解决这个问题。

playermovement function isn't part of a rigidbody it is part of script that is attached to a gameObject so each gameobject that wants to call this must have this script attached to them and you can call their move function just by calling playerMovement() inside the script playermovement函数不是rigidbody的一部分,它是附加到gameObject的脚本的一部分,因此每个想要调用它的游戏对象必须将这个脚本附加到它们上,你可以通过调用其中的playerMovement()来调用它们的move函数。脚本

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

function Start () {
player = 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 Update () {
 playerMovement();
}

and what you wrote got another problem and that is you are moving your both players with the same keys at the same times , so you have to make separate keys for each players input or make it turn based , it is up to your game 你写的东西还有另外一个问题,就是你在同一时间用同一把钥匙移动你的两个玩家,所以你必须为每个玩家输入制作单独的按键或让它转向,这取决于你的游戏

public var speed : int = 15;
var player1Obj: GameObject;
var player2Obj: GameObject;
var player : Rigidbody;
var player2 : Rigidbody;

function Start () {
player = player1Obj.GetComponent.<Rigidbody>();
player2 = player2Obj.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.W)){
    player2.AddForce(Vector3.forward*speed);
    Debug.Log("Player 2 is moving forward");
}
if(Input.GetKey(KeyCode.S)){
    player2.AddForce(Vector3.back*speed);
}
if(Input.GetKey(KeyCode.A)){
    player2.AddForce(Vector3.left*speed);
}
if(Input.GetKey(KeyCode.D)){
    player2.AddForce(Vector3.right*speed);
}

} 

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

You need to pay attention when porting your code from C# to Javascript. 将代码从C#移植到Javascript时需要注意。

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

The functions you are calling are both in the same script. 您调用的函数都在同一个脚本中。

Just call them directly. 直接打电话给他们。

playerMovement();
player2Movement();

Also notice the bad spelling change in the code above player 2 Movement. 另请注意播放器2运动上方代码中的拼写错误更改。

Even when you fixed that, it will compile with no error but won't work as expected because of: 即使你修复了它,它也会编译没有错误,但由于以下原因无法正常工作:

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

Since you have different GameObject/balls to move. 因为你有不同的GameObject /球要移动。 You have to use GameObject.Find to find them before getting the Rigidbody reference of each of them. 在获取每个参考的Rigidbody参考之前,您必须使用GameObject.Find来查找它们。

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

Finally, the controls are messed up. 最后,控件搞砸了。 You are using the-same control. 您正在使用相同的控件。 I think this is a bad copy and paste or something but below is your whole working code. 我认为这是一个糟糕的复制和粘贴或其他东西,但下面是你的整个工作代码。

Note : 注意

Before you say it doesn't work, make sure that that your two GameObject/balls are named Ball1 and Ball2 and also make sure that the old C# cold is not attached to them. 在你说它不起作用之前,确保你的两个GameObject /球被命名为Ball1Ball2 ,并确保旧的C#冷没有附加到它们。 The code was tested and it works! 代码经过测试,确实有效!

#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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript switch 语句给了我一个错误。 到底是怎么回事? - Javascript switch statement is giving me an error. what is going on? Windows 脚本主机在运行 JavaScript 时出错 - Windows Script Host is giving an error running JavaScript 不知道为什么这段代码给我一个错误。 “我”似乎没有定义,有人可以解释这一点 - Not sure why this code is giving me an error. “i” seems not to be defined, can someone explain this 为什么这个JavaScript代码给我错误“未定义”? - Why is this JavaScript code giving me error “undefined”? Javascript代码在Chrome和Firefox上给出错误 - Javascript Code giving error on chrome and firefox Javascript代码在控制台中给出条件错误 - Javascript code giving if condition error in console 我正在运行这个 javascript 代码,但它给了我一个..错误请告诉我出了什么问题,因为我认为代码是正确的? - I am running this javascript code but it is giving me an.. error please tell me what is wrong because I think the code is correct? 在页面加载上使用spinnerPlugin会出错。 - Using spinnerPlugin on page load is giving error. javascript错误。 未捕获的ReferenceError - javascript error. uncaught ReferenceError 从代码笔复制的 JavaScript 代码出现错误 - JavaScript code copied from code pen giving error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM