Comments and answers for "getaxis and getkey don't work simultaneously"http://answers.unity.com/questions/1537615/getaxis-and-getkey-and-getkeyup-dont-work-simultan.html
The latest comments and answers for the question "getaxis and getkey don't work simultaneously"Answer by Dilaxer01http://answers.unity.com/answers/1537648/view.html
Guys, I figured it out. The problem was: apparently control works only when combined with other keys. Since I was pressing only control, I guess it caused complications between pressing control and a, d (axes) simultaneously. I could have added a key to be pressed with control, but instead I just changed control to another key (Z) and now it works. I can move AND hold ctrl for when I wanna run. Thanks for help, @Nebukam @Bunny83Thu, 02 Aug 2018 23:04:17 GMTDilaxer01Comment by Dilaxer01 on Dilaxer01's answerhttp://answers.unity.com/comments/1537640/view.html
@Nebukam That didn't fix my problem, but it made my code cleaner because apparently I didn't even need a bool speedMultiplyWhenRunningAlreadyHappened, I just needed to change GetKey to GetKeyDown. Problem is I wanna move and while moving press ctrl and become faster, release ctrl and go back to normal speed, but now when I press ctrl I just stop and get faster (but I can't move while pressing ctrl so getting faster is useless) and when I release it I can move again (at regular speed)Thu, 02 Aug 2018 21:54:36 GMTDilaxer01Answer by Bunny83http://answers.unity.com/answers/1537624/view.html
The easiest and most reliable solution is just this:
void FixedUpdate ()
{
moveInput = Input.GetAxis("Horizontal");
if (Input.GetKey(KeyCode.LeftControl))
moveInput *= 2f;
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}
You shouldn't multiple and divide floating point numbers all the time as they might drift away over time. This may not happen with a factor of "2" but odd numbers could result in slight errors over time. In my case we just conditionally muliply the moveInput by 2. Since moveInput is read / updated every fixed frame we get a stable result.
<br>
If you want to have a speed variable that holds the current speed (for some reason) you shouldn't mess with your variables that you use as setting in the inspector. Instead use a seperate variable.
// set those in the inspector, don't mess with them in code
public float walkSpeed;
public float sprintMultiplier = 2;
// This will hold your actual speed:
private float speed;
void FixedUpdate ()
{
speed = walkSpeed;
if (Input.GetKey(KeyCode.LeftControl))
speed *= sprintMultiplier;
moveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}Thu, 02 Aug 2018 21:54:32 GMTBunny83Comment by Nebukamhttp://answers.unity.com/comments/1537617/view.html
Just to be sure : your FixedUpdate function is not closed with an }, is this a typo in your code too or just in the question ?Thu, 02 Aug 2018 21:40:51 GMTNebukamAnswer by Nebukamhttp://answers.unity.com/answers/1537616/view.html
This may not work since your code is missing some parts (your speedMultiplyWhenRunningAlreadyHappened may be used somewhere else ?), but try changing the content of you Update() function with the following one :
private void Update()
{
if ( Input.GetKeyDown(KeyCode.LeftControl))
speed *= 2;
else if( Input.GetKeyUp(KeyCode.LeftControl))
speed /= 2;
}
Note the difference; **GetKeyDown** return true if the key was previously up and has been pressed this frame (so you can spare the use for the boolean), while GetKey tells you whether or not the key is currently held down.Thu, 02 Aug 2018 21:39:20 GMTNebukam