- Home /
Can I change the FPC so that you have to repeatedly press a key to walk?
I'm making a walkthru game that utilizes a dancemat so that you have to actually "walk" instead of just holding down the "W" key. I have the dancemat emulating the keyboard so I'm good there. I just need a slight tweek so that the FPC keeps moving when the key is rcontinually pressed and released. If the player holds the "W" key down for 1 second OR releases it for 1 second the FPC should stop. I know there is probably a lot of work involved, but, just a point in the right direction would be greatly appreciated. Do I need to modify the FPSInput Controller Script, the Character Motor Script, neither one, or start over from scratch with something new? Thanks
Answer by Owen-Reynolds · Mar 22, 2013 at 03:15 PM
The GetAxis command uses hidden Axis variables, maintained internally by Unity, from key presses. They magically rise to 1 when held and sink to 0 when released. You can just make your own "Axis" variables the same way. The built-in works something like this:
if( Input.getKey("right")) { // held down
if(hAxe<0 && snap==true) hAxe=0; // snap turn
hAxe += hSpeed * Time.deltaTime; // speed up when held
if(hSpeed>1) hSpeed=1; // axises go from -1 to 1
}
..... // same for left (skipping)
else // no key held -- slow down when not held
hSpeed = Mathf.MoveTowards(hSpeed, 0, hGravity * Time.deltaTime);
I don't quite understand your "1 second rule," but, say you wanted a key to count for up to one second:
float rightActiveTime;
if(Input.GetKeyDown("right")) // press -- mark the time
rightActiveTime=Time.time+1.0f;
if(Input.GetKey("right") && Time.time < rightActiveTime)
// key has been held for less than 1 second
Thank you very much that looks awesome. I need time to fully study and understand it. The 1 second rule was just a random time. If someone stops on the mat they are essentially standing on or pressing the "W" key. That would normally make an FPC move forward, but in the walkthru they are stopped so I can't have any movement. The same goes for if they leave the mat. The key is up and there should be no movement of the FPC. The only thing that makes the FPC move is the constant cycling of the key. Is that what you have so generiously given me?
Well, you have the ability to detect "just pressed", "just let go" and "currently held down." You can use that to build whatever you want.
For a "keep clicking to run" you might use the trick where you remember the last time they clicked, and within 1 second means they are running.
Thanks. I wasnt aware if thise functions. I'll check it out.
Your answer
Follow this Question
Related Questions
Animating CharacterController When Stepping Up 0 Answers
Unity Flash, character jumps when the controller touches a step 0 Answers
My character won't move in C#. 1 Answer
Hi, Could someone tell me why this is reversed? 1 Answer
MMD How to export model and animations to Unity as 3rd person controller? 2 Answers