Using preferred keys to move a player in unity

2019-07-30 05:09发布

I am trying to complete roll a ball tutorial (https://unity3d.com/learn/tutorials/projects/roll-ball-tutorial) in a different way by adding two balls. So two players can play the game. But the problem i am facing is that i want to configure the preferred keys for the second player like the firsl player uses the traditional arrow keys and the second player use w,a,s,d to move up left down right... my c-sharp code for the first player is this...

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

Let me know if anyone have solution

2条回答
Melony?
2楼-- · 2019-07-30 05:36

Answered Similar question here.

The easiest solution that will require you not modify your key controls is to not use Input.GetAxis at-all. Detect each key press with Input.GetKey() and their keycodes enum. Problem solved! Now assign the two balls from the Editor. You can easily modify it to work with one ball if that's what you want.

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();
    }
}
查看更多
姐就是有狂的资本
3楼-- · 2019-07-30 05:42

You can define more inputs in edit -> project settings -> Input. To add more inputs just increase the size value and config the new values in. At least input name and keys to the new inputs. Lastly, in your code call the new inputs for player 2 with the names you specified in the project settings.

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);
}
查看更多
登录 后发表回答