Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by phantom24 · Jun 07, 2012 at 10:32 AM · cargetaxiswasd

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges