I want to progressively increase a variable every frame,but my code is only increasing it once

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Harmonikinesis

I want to make a pseudo-3D game using the 2D part of the engine (like M&L Superstar Saga) and to do so I need to code a depth variable (a Z direction if you will). However,even though I wrote the movement code and the depth code in the same function,while the movement code progressively adds more to a variable every frame,the jump code only adds to the variable once and keeps the value the same as long as the action is being executed. What is the issue with my code?

extends KinematicBody2D

var speed = 20
var stop_speed = 60
var top_speed1 = 200
var top_speed2 = -200
var gravity = 10
var jumpower = 20
var z_position
var x_math
var y_math
var x_direction = 1
var y_direction = 1
var motion = Vector2()
var up = 0
var down = 0
var left = 0
var right = 0

func _physics_process(delta):
motion = move_and_slide(motion,motion)
var input_up = Input.is_action_just_pressed(“Up”)
var input_down = Input.is_action_pressed(“Down”)
var input_left = Input.is_action_pressed(“Left”)
var input_right = Input.is_action_pressed(“Right”)
var jump = Input.is_action_pressed(“Jump”)

if !input_up and y_direction == -1 and motion.y < 0:
	motion.y = 0
if !input_down and y_direction == 1 and motion.y > 0:
	motion.y = 0
if !input_left and x_direction == -1 and motion.x < 0:
	motion.x = 0
if !input_right and x_direction == 1 and motion.x > 0:
	motion.x = 0


if input_up:
	up = 1
	motion.y -= speed
else:
	up = 0
	motion.y += stop_speed
if input_down:
	down = 1
	motion.y += speed
else:
	down = 0
	motion.y -= stop_speed
if input_left:
	left = 1
	motion.x -= speed
else:
	left = 0
	motion.x += stop_speed
if input_right:
	right = 1
	motion.x += speed
else:
	right = 0
	motion.x -= stop_speed



if motion.x > top_speed1:
	motion.x  = top_speed1
if motion.x < top_speed2:
	motion.x = top_speed2
if motion.y > top_speed1:
	motion.y  = top_speed1
if motion.y < top_speed2:
	motion.y = top_speed2


x_math = right - left
if !(x_math == 0):
	x_direction = x_math


y_math = down - up
if !(y_math == 0):
	y_direction = y_math


z_position =- gravity

if z_position < 0:
	z_position = 0
if jump:
	z_position += jumpower
print(motion)
:bust_in_silhouette: Reply From: jgodfrey

Here, you’re setting the z_position to negative gravity each frame:

z_position =- gravity

So, here, it’ll always be less than zero, so it’ll be assigned to zero:

if z_position < 0:
    z_position = 0

So, if jump is pressed here, z_position will always be -gravity + jumppower which will always be the same value…

if jump:
    z_position += jumpower

While I’m not entirely sure what you want here, I assume you meant this:

z_position =- gravity

to instead be:

z_position -= gravity

Which subtracts gravity from z_position each frame, rather than setting z_position to negative gravity each frame.

I tried to use “-=” instead of “=-” before,but I would get an error and the game wouldn’t run.
I then realized that it was not working because I needed to assign a value to z_position,which I didn’t.

Thanks for you help regardless.

Harmonikinesis | 2020-08-06 18:50