Attention | Topic was automatically imported from the old Question2Answer platform. | |
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)