x


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!

more ▼

asked Jun 07 '12 at 10:32 AM

phantom24 gravatar image

phantom24
32 1 2 3

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
}
more ▼

answered Jun 07 '12 at 11:06 AM

aldonaletto gravatar image

aldonaletto
42.5k 16 43 202

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x424
x37
x36

asked: Jun 07 '12 at 10:32 AM

Seen: 1726 times

Last Updated: Jun 07 '12 at 11:06 AM