- Home /
split screen multiplayer car game?
Hello, I'm creating a car game, I was thinking of adding a split screen multiplayer game feature. I'm stuck at a point, I need help real badly, I need to submit this work within 7 days. I have added two cars in a scene which use the same script, but the problem is both are controlled by the same keys. When i press the UP ARROW or the key W both the cars move, tried to edit the key settings from Edit>Project settings> Input. But again both the car controls were edited. I want one car to be controlled by W,S,A,D and other with U,J,H,K. Somebody please help me with this.
P.S : throttle = Input.GetAxis("Vertical"); steer = Input.GetAxis("Horizontal");
This is the part of the code which gets the input.
Add the input-string to a variable. This will allow you to define unique input to each different car which means that each car will not be controller by both users.
EG:
var throttleInput : String; //"Vertical" for car 1 and "Vertical 2" for the second car.
Just remember to have these inputs defined in the input settings.
Answer by Novodantis · Feb 12, 2013 at 11:48 AM
You can create new inputs in the Input Manager by increasing the number at the top. Copy the buttons & axis' that you need and name them "Horizontal 2", "Vertical 2" etc.
Then add a variable on the car to define which player controls them. You can then have a condition in your code that decides which set of controls to accept. An example (js):
var playerID : int; // the player whose car this is
var acceleration : float; // throttle (replace this, it's just to illustrate)
function Update() {
if (playerID == 1) {
// player 1 controls, eg
acceleration = Input.GetAxis("Vertical");
} else if (playerID == 2) {
// player 2 controls
acceleration = Input.GetAxis("Vertical 2");
}
}
Don't forget to set the value of Player ID in the inspector, to the player that controls a given car. Please note there is large room for improvement here, but I hope it illustrates the principle.
Answer by Shaolin-Dave · Apr 18, 2015 at 04:48 PM
For more expandability, try this...
function Update() {
if (playerID == 1) {
// player 1 controls, eg
acceleration = Input.GetAxis("Vertical");
} else {
// all other player controls
acceleration = Input.GetAxis("Vertical " + playerID);
}
}
And if you can rename "Vertical" to "Vertical 1" (might require more research), this would be even better.
function Update() {
acceleration = Input.GetAxis("Vertical " + playerID);
}