简体   繁体   中英

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'.

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'. 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

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.

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.

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. You have to use GameObject.Find to find them before getting the Rigidbody reference of each of them.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM