//下面是我到目前爲止的代碼。無論我使用哪種控制,我的兩個球都在同時移動。有人能幫我伸出援助之手嗎?我如何阻止我的兩名球員同時移動?
public class PlayerController : MonoBehaviour
{
public float speed = 80.0f; // Code for how fast the ball can move. Also it will be public so we can change it inside of Unity itself.
public GameObject player1; //Player 1 Rigidbody
public GameObject player2; //Player 2 Rigidbody
private Rigidbody rb;
private Rigidbody rb2;
void Start()
{
rb = GetComponent<Rigidbody>();
rb2 = GetComponent<Rigidbody>();
player1 = GameObject.Find("Player");
player2 = GameObject.Find("Player 2");
}
//Player 1 Code with aswd keys
void Player1Movement()
{
if (player1 = GameObject.Find("Player"))
{
if (Input.GetKey (KeyCode.A)) {
rb.AddForce (Vector3.left * speed);
}
if (Input.GetKey (KeyCode.D)) {
rb.AddForce (Vector3.right * speed);
}
if (Input.GetKey (KeyCode.W)) {
rb.AddForce (Vector3.forward * speed);
}
if (Input.GetKey (KeyCode.S)) {
rb.AddForce (Vector3.back * speed);
}
}
}
//Player 2 Code with arrow keys
void Player2Movement()
{
if(player2 = GameObject.Find("Player 2"))
{
if (Input.GetKey(KeyCode.LeftArrow))
{
rb2.AddForce(Vector3.left * speed);
}
if (Input.GetKey(KeyCode.RightArrow))
{
rb2.AddForce(Vector3.right * speed);
}
if (Input.GetKey(KeyCode.UpArrow))
{
rb2.AddForce(Vector3.forward * speed);
}
if (Input.GetKey(KeyCode.DownArrow))
{
rb2.AddForce(Vector3.back * speed);
}
}
}
// Update is called once per frame
void Update()
{
Player1Movement();
Player2Movement();
}
}
我如何改變它,我的兩個球員都沒有在同一時間運動?
有人可以幫我嗎? –