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
0
Question by LostTrainDude · Feb 08, 2013 at 10:48 PM · buttoncharactermultipleinputmanagerrunning

How to make a character run by alternately hitting two buttons?

As for old games such as Konami's Track and Field, where the feedback you get is that "the faster you hit, the faster you run", I'd like to know if it's possible to get such mechanic to work in Unity and how.

I don't know if it's possible to "override" the InputManager somehow or if it's possible to create the mechanic within the default CharacterController (right now I'm testing it on the 3D 3rd Person one, but I'm looking forward to develop a 2D side-scrolling kind of game)


I'm a true beginner to both JS/C# scripting and Unity, so I apologize if I appear lazy or unappropriate, I don't mean it at all! :)
I'm asking because I really can't easily figure where to begin.

Actually, I do have a little scripting background (mainly because of Adventure Game Studio) and a little knowledge of UDK environment, so I MAY (hopefully) understand a little more than expected by my premise.

I'm trying to get acquainted to Unity as much as I can.
I'll appreciate any pointer you may give me :D

Thanks a lot!

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

3 Replies

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

Answer by DeBunked · Feb 08, 2013 at 11:47 PM

Use a script to check for alternating states of a key. This can be done by simply checking for an actual press-in of the key rather than whether the key is being pressed.

This comes down to the difference between Input.GetKey and Input.GetKeyDown. The first returns true every frame/time whereas the latter returns true only if there is a change in state (from up to down), ruling out a scenario where the key is being pressed uninterruptedly.

 if (Input.GetKeyDown(KeyCode.W)) //or any other key you want to use as movement trigger
    {
       transform.position += transform.forward * speed * Time.deltaTime;
    }

As to editting the character controller to do this, it's possible but you would have to apply the same basic idea to the conditionals it uses to check for input.

EDIT: Woops, forgot the 2-key part. One way of doing this would be:

 if (Input.GetKeyDown("key 1") && keyAlternate == false)
   {
     transform.position += transform.forward * speed * Time.deltaTime;
     keyAlternate = true;
   }
 else if (Input.GetKeyDown("key 2") && keyAlternate == true)
   {
     transform.position += transform.forward * speed * Time.deltaTime;
     keyAlternate = false;
   }

Or the somewhat more sofisticated way that is described below.

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 Owen-Reynolds · Feb 09, 2013 at 12:41 AM

For the alternating, it's just basic programming. Make a var to remember which key you are on, the check that var during the press:

 char moveLetter="a"; // alternate a,s,a,s...

 // update:
 if(Input.GetKeyDown(moveLetter)) {
   // got that key, switch the key we need for next time:
   if(moveKey=="a") moveKey="s"; else moveKey="a";

   // get a burst of speed

The other part is, the standard controller sets your speed directly from the arrow or WS keys. You'd have to change that so it doesn't recalc speed each frame. Instead it adds to speed for each key press, and remembers when the last key was pressed. If it's been more than 1/2-sec, slow down.

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 LostTrainDude · Feb 10, 2013 at 12:50 AM

Thanks to you both! I'm finally gettin' something :D

At the moment, even if I think I understood what Owen Reynolds wrote, it felt easier, to me, to follow the "less sophisticated" one, by DeBunked since I actually tried something similar but "lost myself" through if statements.

Anyway, I'm now getting him to run horizontally on the x axis, but I suppose that I have to code something for the colliders, since it works nice on a Z = 0 rotated surface, but if I try to get him run over a Z = 30 one, it clips through it.

I've also noticed that changing

 transform.position += transform.forward * speed * Time.deltaTime;

to

 transform.position.x += transform.position.x * speed * Time.deltaTime;

seems to boost the character speed each time the key is pressed, but I believe it only multiplies his position on the x axis, so he's not "running" as we mean it.

Each time I encounter a Time.deltaTime it always has some sort of "wow" effect, because I still have to really understand what it actually means (even if I read the documentation). In this very case, does it mean that it's multiplying it's position.x by the value of the speed variable by the time that past between one click and another (if a frame can be intended in such way, in this case)?

Thanks a lot, so far!

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 Owen-Reynolds · Feb 10, 2013 at 05:18 PM 0
Share

Look around for how to move a player. No matter how speed gets sets, moving is all pretty much the same, and explains why Time.delTime might be useful. Yes, doing it the way you are will ghost right through obstacles.

avatar image LostTrainDude · Feb 10, 2013 at 07:05 PM 0
Share

I'll keep you updated as I achieve something! Thanks again :D

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

12 People are following this question.

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

Related Questions

Cannot assign GameObject in Inspector? [SOLVED] 2 Answers

i want to create a small game 1 Answer

Get (positive) button names from Inputmanager? 0 Answers

Xbox Controller & input manager? 1 Answer

How to make the character strafe while running? 1 Answer


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