Hello,
I have been attempting to use CharacterControllers on my two player Shoot 'Em Up game, and thought I may as well use CharacterController.Move to move the players around the field.
I had a look at the documentation on this, and have implemented the example code into my code. The only problem is, it uses Input.GetAxis Horizontal and Vertical, so when I move one player, the other one will move as well (It happens currently when both player movement keys are pressed, separately it works fine as the if statement handles that).
Is there a way I can handle both players using the GetAxis Horizontal and Vertical? I have tried every second Update() to move one character alternatively, and have also rewritten the movement so that if W is pressed, transform.position.x + 1 * speed * Time.deltaTime
etc, but this keeps moving the player in the direction I pressed until I press another key, then the character slows down the goes in the opposite direction (may be my error in coding but all looked good), so I would much prefer to have the GetAxis Horizontal and Vertical working instead. Here is my code below:
//Player starting position
public Vector3 playerTwoPosition = new Vector3(0, 0, 0);
//The CharacterControllers
CharacterController playerOne;
CharacterController playerTwo;
public float speed = 10.0f;
void Start() {
playerOne = GameObject.FindGameObjectWithTag("Player 1").GetComponent<CharacterController>();
playerTwo = GameObject.FindGameObjectWithTag("Player 2").GetComponent<CharacterController>();
}
void Update() {
if (this.gameObject.tag == "Player 1") {
if (Input.GetKey("w") || Input.GetKey("a") || Input.GetKey("s") || Input.GetKey("d")) {
playerOnePosition = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
playerOnePosition = transform.TransformDirection(playerOnePosition);
playerOnePosition *= speed;
}
}
if (this.gameObject.tag == "Player 2") {
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.RightArrow)) {
playerTwoPosition = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
playerTwoPosition = transform.TransformDirection(playerTwoPosition);
playerTwoPosition *= speed;
}
}
playerOne.Move(playerOnePosition * Time.deltaTime);
playerTwo.Move(playerTwoPosition * Time.deltaTime);
}
Thank you in advance for you help and insight,
Daniel.