- Home /
Car control with WASD keys or arrow keys
I have the basic car control script:
var speed : float = 10.0;
var rotationSpeed : float = 100.0;
function Update () {
// Get the horizontal and vertical axis.
// By default they are mapped to the arrow keys.
// The value is in the range -1 to 1
var translation : float = Input.GetAxis ("Vertical") * speed;
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
// Make it move 10 meters per second instead of 10 meters per frame...
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
// Move translation along the object's z-axis
transform.Translate (0, 0, translation);
// Rotate around our y-axis
transform.Rotate (0, rotation, 0);
}
The Input.GetAxis("Vertical") returns a number value. However, I want to make a multiplayer game. One car controlled by WASD, the other by the arrow keys. My problem is that the GetAxis function uses both the WASD and arrow keys, but I want to differentiate between the two so one car moves based on the number returned by the WASD keys, the other moves based on the number returned by the arrow keys.
Does that make sense? If not, please tell me the problem so I can expand
Thanks in advance!
Answer by aldonaletto · Jun 07, 2012 at 11:06 AM
It's much easier to redefine the axes in the Input Manager (menu Edit/Project Settings/Input): click the Horizontal axis and press Ctrl-D to duplicate it, then change their names to Horizontal1 and Horizontal2 (for instance); in Horizontal1, clear the fields Negative Button and Positive Button fields (initially set to the arrow keys), and in Horizontal2 clear the fields Alt Negative Button and Alt Positive Button (initially set to A and D). Repeat the process for the Vertical axis, and presto! you've got two horizontal and vertical axes.
NOTE: remember to use the appropriate axis name for each player, maybe setting variables in the Inspector:
var speed : float = 10.0; var rotationSpeed : float = 100.0; var vertAxis = "Vertical1"; // change to "Vertical2" for player 2 var horAxis = "Horizonatal1"; // change to "Horizontal2" for player 2
function Update () { // Get the horizontal and vertical axis. // By default they are mapped to the arrow keys. // The value is in the range -1 to 1 var translation : float = Input.GetAxis (vertAxis) speed; var rotation : float = Input.GetAxis (horAxis) rotationSpeed;
// Make it move 10 meters per second instead of 10 meters per frame...
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
// Move translation along the object's z-axis
transform.Translate (0, 0, translation);
// Rotate around our y-axis
transform.Rotate (0, rotation, 0);
}
Answer by mnmwert · Apr 19, 2016 at 02:46 AM
you can also use
if(Input.GetKey(KeyCode.W)
this will make that movement confined to that button. I would not recommend using this though, as it can be cumbersome and is not a readable. but it does work as i have used it before.
This is for Unity Version 5.2
Your answer
Follow this Question
Related Questions
Use only one control scheme for Input.GetAxis 1 Answer
Car acceleration using Xbox 360 triggers? 1 Answer
Extremely simple car movement 1 Answer
WheelColliders causing my wheels to hang in the air 2 Answers
unity car tutorial help 0 Answers