Increasing the Variable's value

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Noob_Maker
:warning: Old Version Published before Godot 3 was released.

I want to increase the speed variable for whenever the player presses shift. I added the control and got the script saying if Sprint == true: s+700. Sprint is the variable for the command for making sure that the shift key is pressed. I added it in but I only could go left. I did a similar method but implement an if statement sequence that looks like this:

if Right == true && Sprint == true:
	set_linear_velocity(Vector2 (s + 700, get_linear_velocity().y))
elif Left == true && Sprint == true:
	set_linear_velocity(Vector2(-s + -700, get_linear_velocity().y))
else: 
	set_linear_velocity(Vector2 (0, get_linear_velocity().y))

But the player sprite doesn’t move at all. Is there something wrong or what?

I assume that’s not the actual code, because the functions should have underscores in their name.

And what exactly are the values inside Right, Left, and Sprint the moment it enters this branch?

avencherus | 2016-11-13 18:33

The title of your question does not really reflect the issue you actually present.

If your real issue is that your player doesn’t move, that could be caused by many things.

Try breaking your down down into three steps:

  • get stuff (player velocity, inputs, etc)
  • do stuff (using the stuff you get)
  • set stuff (apply any changes you made while doing stuff)

Real simple code example:

# get stuff
var SPEED = 100
var RUN_SPD = 150
var LEFT = Input.is_action_pressed('left')
var RUN = Input.is_action_pressed('run')
var lv = get_linear_velocity()

# do stuff
if LEFT:  lv.x = -SPEED
    if RUN:  lv.x += sign(lv.x) * RUN_SPD
else: lv.x = 0

# set stuff
if lv != get_linear_velocity():
    set_linear_velocity(lv)

YeOldeDM | 2016-11-13 18:57

I edited original post for better code look.

volzhs | 2016-11-13 20:08

Not sure if is the case but try to be sure that the keys are being pressed (or detected as pressed).

Some combinations don’t work as one may expect with special keys.

Like, the action “Shift+Left” is not the same as “action Shift” + “action Left”

eons | 2016-11-13 20:12

I have no problem with the player movement in general, It’s just I’m trying to figure out how to increase the speed value for the player to dash. Originally I wanted to have a button that does that(as in holding Shift to run) but then that didn’t work because the engine thought that I was holding Shift while I wasn’t. Right now I’m thinking of double tapping the movement keys to dash. So yeah…

Noob_Maker | 2017-02-22 15:01