0
\$\begingroup\$

Good afternoon, I have a problem with an input in my code: Before testing it on mobile I tried on PC and the code to make my player sprint was like this:

        var sprintPressed = Input.GetKey(KeyCode.LeftShift);
        var targetSpeed = maxSpeed;
        if (attacking) {
            targetSpeed *= attackMoveSpeedMultiplier;
        }
        else if (sprintPressed) {

            targetSpeed *= sprintMultiplier;
        }

And everything was working fine. My player increases the movement speed as soon as I press "Left Shift" button. Then I changed the code to make it work with a mobile button:

        var sprintPressed = CrossPlatformInputManager.GetButton("Sprint");
        var targetSpeed = maxSpeed;
        if (attacking) {
            targetSpeed *= attackMoveSpeedMultiplier;
        }
        else if (sprintPressed) {
            Debug.Log(sprintPressed ? sprintMultiplier : 1);//just to see if it take a button
            targetSpeed *= sprintMultiplier;
        }

I also make a change for a controller so instead, to press WASD I use a screen joystick. That's work fine. But when I press a button to make a sprint, nothing happens. I insert a debug log line and when I press a button it just return 1 single value (sprintMultiplier) and not 1 per frame as it should be because this function is in the Fixed Update. I check in Google and I found this tutorial https://www.youtube.com/watch?v=nGYObojmkO4&t=851s&list=PLXVRS21ln3JIbFLPwf9bdH3thHmmYRJGi&index=16 Pretty much what I did but in his video, it's working. In my project doesn't. Can someone help me, please? Many thanks beforehand

\$\endgroup\$
0
\$\begingroup\$

Silly me. You can close this post as I solved the problem. I just swap the "if (attacking)" and the "else if" and everything is working fine. I don't know why it doesn't like to have a sprint after the attack lol.

\$\endgroup\$
1
  • \$\begingroup\$ If your question is solved, mark this answer as correct ,so it doesn't appear in the front page. \$\endgroup\$
    – TomTsagk
    Aug 22 '19 at 14:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .