Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Maniac567 · Oct 29, 2015 at 01:24 AM · c#movementkeypresskeycodewasd

problem with mapping keys through c#

I am doing a continuation on Unity's tutorial Roll A Ball, for a programming class, in which I have two balls that need to be controlled through different key inputs (ideally one being controlled through the arrow keys and the other one controlled by the wasd keys). However, my instructor explicitly said to not use the input manager in Unity. I am at a loss and need help on what I should do.

This code controls a ball, but I need it to be controlled with the wasd keys:

     public float speed;
     public Text winText;
     Vector3 startPos;
     private Rigidbody rb;
 
     void Start ()
     {
         rb = GetComponent<Rigidbody> ();
         startPos = transform.position;
         winText.text = "";
     }
 
     void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
 
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
 
         rb.AddForce (movement * speed);
     }
 
     void OnTriggerEnter (Collider other)
     {
         if (other.gameObject.CompareTag ("Reset")) {
             rb.velocity = new Vector3 (0f, 0f, 0f);
             rb.angularVelocity = new Vector3(0f, 0f, 0f);
             transform.rotation = Quaternion.Euler (new Vector3(0f, 0f, 0f));
             transform.position = startPos;
         }
         if (other.gameObject.CompareTag ("Enemy")) {
             rb.velocity = new Vector3 (0f, 0f, 0f);
             rb.angularVelocity = new Vector3(0f, 0f, 0f);
             transform.rotation = Quaternion.Euler (new Vector3(0f, 0f, 0f));
             transform.position = startPos;
         }
         if (other.gameObject.CompareTag ("Goal")) {
             winText.text = "Player 1 wins!";
             gameObject.SetActive (false);
             other.gameObject.SetActive (false);
         }
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by OncaLupe · Oct 29, 2015 at 03:44 AM

This seems weird to me, since avoiding the input manager means you need to hard code inputs unless you make your own rebinding system in game. Hopefully your instructor is doing this for a reason, so I'll just go with it for now. The simplest way is to look for specific KeyCodes like this;


if(Input.GetKey(KeyCode.W))
{
  //Key 'W' is being held down
}

Check here for more info:

http://docs.unity3d.com/ScriptReference/Input.GetKey.html http://docs.unity3d.com/ScriptReference/KeyCode.html

Comment
Add comment · Show 2 · 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 Maniac567 · Oct 29, 2015 at 01:33 PM 0
Share

@OncaLupe Funny enough, I have been trying to do that before I posted this question. I have been experimenting with it, but I'm never clear as to what I should put between the curly brackets. Looking at pseudo code doesn't help me much.

avatar image OncaLupe · Oct 29, 2015 at 03:58 PM 0
Share

@$$anonymous$$aniac567 Unfortunately if I answered that I'd basically be doing the project for you. Basically though what you'll want to do is use the AddForce method kind of like how the sample code you linked does, but the values put in will depend on which key is being pressed. For when 'W' is pressed you'd add Z force, 'S' is negative Z force, etc.

http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

How to get Key with KeyCode.Question? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

I want to play an Audio Sound for a character while running. 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