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