2016-05-31 9 views
0

두 개의 공을 추가하여 다른 방법으로 공을 굴리기 위해 연습하고 있습니다 (https://unity3d.com/learn/tutorials/projects/roll-ball-tutorial). 두 명의 플레이어가 게임을 할 수 있습니다. 하지만 내가 직면하고있는 문제는 firsl 플레이어가 전통적인 화살표 키를 사용하고 두 번째 플레이어가 w, a, s, d를 사용하여 오른쪽 아래로 왼쪽으로 이동하는 것처럼 두 번째 플레이어의 기본 설정 키를 구성하려고한다는 것입니다. 첫번째 선수에 대한 내 C 날카로운 코드는이 ...기본 키를 사용하여 플레이어를 이동하십시오

using UnityEngine; 
using System.Collections; 

public class PlayerController : MonoBehaviour { 

public float speed; 

private Rigidbody rb; 
void Start() 
{ 
    rb = GetComponent<Rigidbody>(); 
} 

void FixedUpdate() 
{ 
    float moveHorizontal = Input.GetAxis("Horizontal"); 
    float moveVertical = Input.GetAxis("Vertical"); 

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);    //Values for movement vector 3 takes three arguments like x y z for positions. 

    rb.AddForce (movement * speed); 
} 
} 

사람이 솔루션을

+0

공이 위아래로 움직이지 않습니다. 앞으로, 뒤로, 왼쪽, 오른쪽을 의미합니까? – Programmer

+0

이동이 문제가 아니라 문제는 각 플레이어의 이동에 대해 서로 다른 키를 설정하는 것이므로 두 플레이어가 동일한 키보드를 별도로 제어 할 수 있습니다. – Sami

답변

2

답변 됨 비슷한 질문 here.

키 컨트롤을 수정하지 않아도되는 가장 쉬운 해결책은 Input.GetAxis을 전혀 사용하지 않는 것입니다. Input.GetKey() 및 해당 keycodes enum으로 각 키를 누릅니다. 문제 해결됨! 이제 편집기에서 두 개의 공을 할당하십시오. 그것이 원한다면 하나의 공으로 작동하도록 쉽게 수정할 수 있습니다.

using UnityEngine; 
using System.Collections; 

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 Rigidbody player1RB; //Player 1 Rigidbody 
    public Rigidbody player2RB; //Player 2 Rigidbody 


    //Player 1 Code with aswd keys 
    void Player1Movement() 
    { 
     if (Input.GetKey(KeyCode.A)) 
     { 
      player1RB.AddForce(Vector3.left * speed); 

     } 

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

     } 

     if (Input.GetKey(KeyCode.W)) 
     { 
      player1RB.AddForce(Vector3.forward * speed); 

     } 

     if (Input.GetKey(KeyCode.S)) 
     { 
      player1RB.AddForce(Vector3.back * speed); 

     } 
    } 

    //Player 2 Code with arrow keys 
    void Player2Movement() 
    { 
     if (Input.GetKey(KeyCode.LeftArrow)) 
     { 
      player2RB.AddForce(Vector3.left * speed); 

     } 

     if (Input.GetKey(KeyCode.RightArrow)) 
     { 
      player2RB.AddForce(Vector3.right * speed); 

     } 

     if (Input.GetKey(KeyCode.UpArrow)) 
     { 
      player2RB.AddForce(Vector3.forward * speed); 

     } 

     if (Input.GetKey(KeyCode.DownArrow)) 
     { 
      player2RB.AddForce(Vector3.back * speed); 

     } 
    } 

    // Update is called once per frame 
    void FixedUpdate() 
    { 
     Player1Movement(); 
     Player2Movement(); 
    } 
} 
1

사항이 있으면 당신은 edit -> project settings -> Input에서 더 많은 입력을 정의 할 수 있습니다 알려 것입니다. 더 많은 입력을 추가하려면 크기 값을 높이고 새 값을 설정하십시오. 최소한 새 입력에 이름과 키를 입력하십시오. 마지막으로, 코드에서 플레이어 2에 대한 새 입력을 프로젝트 설정에서 지정한 이름으로 호출합니다.

void FixedUpdate() 
{ 
    //float moveHorizontal = Input.GetAxis("Horizontal"); 
    //float moveVertical = Input.GetAxis("Vertical"); 

    // example for player 2 
    float moveHorizontalPlayer2 = Input.GetAxis("HorizontalPlayer2"); 
    float moveVerticalPlayer2 = Input.GetAxis("VerticalPlayer2"); 

    Vector3 movement = new Vector3 (moveHorizontalPlayer2 , 0.0f, moveVerticalPlayer2); 

    rb.AddForce (movement * speed); 
} 
+0

사전 정의 된 입력에 사용자가 사용할 대체 키가 있으면 제거해야합니다. 확실하지는 않지만 가로/세로가 각각 a/d/left/right 및 s/w/down/up으로 구성되어 있다고 생각합니다. –

관련 문제