For all Unity developers and developers-to-be, both beginners and professionals!
How to make input keys for two players on the same screen (for the splitscreen multiplayer mode.)
Posted by feillyne on Oct 17th, 2010
After starting a Unity project (or booting original Unity car-racing one).
1. Select Edit -> Project Settings -> Input
2. In CarEdu, Fire Axes (Fire1, Fire2, Fire3) were removed, and only 11 were left:
Horizontal, Vertical, Handbrake, Mouse X, Mouse Y, Mouse ScrollWheel, Window Shake X, Window Shake Y, Horizontal2, Vertical2, Handbrake2
As you can see, also Jump axes were renamed to Handbrake axes, and instead of having two Horizontal (Horizontal, Horizontal), there is Horizontal and Horizontal2. The number 2 points out which players own which "axes", i.e. keys.
In Horizontal axis Alt Negative Button and Alt Positive Button settings were erased.
In Horizontal2 axis alt Negative and Positive buttons were defined ("a" and "d" keyboard keys), and other options (such as Gravity, Dead, Sensitivity, Snap, etc.) were made after Horizontal.
Also notice change in Joy Num property - it was changed to Joystick 1, and Joystick 2, respectively.
Same was done for Vertical (Vertical, Vertical2) axes.
Also, Handbrakes were set appropriately. You can compare them all with input settings of the original Car-racing tutorial by Unity.
See pictures below to see the everything done.
Mouse X, Mouse Y, Mouse ScrollWheel, Window Shake X, Window Shake Y axes were left untouched.
The entire class Wheel was removed from the Car2 script, since only one class with such a name can exist.
Then look at the code itself of Car2.js. What has changed? Can you spot it? ;-)
As you can see, we need to allow both players to control their respective cars (or characters if you make your own game with the splitscreen multiplayer). So we scroll down to this function:
In this function, there are two different axes for each player, see below.
throttle = Input.GetAxis("Vertical");
steer = Input.GetAxis("Horizontal");
throttle = Input.GetAxis("Vertical2");
steer = Input.GetAxis("Horizontal2");
Vertical2 and Horizontal2 are names of the axes you set earlier in Input Manager. If you renamed your second Horizontal to e.g. Player2Horizontal, and your second Vertical axis to Player2Vertical, you would have to set such axes:
throttle = Input.GetAxis("Player2Vertical");
steer = Input.GetAxis("Player2Horizontal");
So proper keys are mapped to proper players.
You need to set handbrakes right, too. You need to find CheckHandbrake() function in Car.js. Below its code and structure:
There's a line that will interest us most:
As you can figure out, there is another button set for the second player.
The "r" keyboard key will be used by the second player to brake.
Also remember to set the Car2 object itself to the Car2.js script, by clicking Car2 object and dragging Car2.js script onto the car, as below:
All done. :-)