I'm trying to create a windy area within which the player would be pushed continuously to the left <-
So far this is what I've come up with for the WindyArea
:
extends Area2D
var bodies_within=[] # saves bodies within the area which have "direction" property
func _physics_process(delta): # the "wind" process
for body in bodies_within:
body.direction.x-=50 # constantly pushing x back by 50
body.direction=body.move_and_slide(body.direction)
func _body_entered(body):
if("direction" in body): # if body has direction then append it to the array
bodies_within.append(body)
set_physics_process(true) # turn on the "wind"
func _body_exited(body):
if(body in bodies_within):
bodies_within.erase(body)
if!(len(bodies_within)): # if no bodies are left within the area then turn off the "wind"
set_physics_process(false)
func _ready():
set_physics_process(false)
self.connect("body_entered",self,"_body_entered")
self.connect("body_exited",self,"_body_exited")
and for my Player body (just for reference, I'm trying not to change this script and having to create a new windy function in it):
extends KinematicBody2D
var direction:Vector2
var gravity_speed=4500
var jump_speed=-1500
var descend_speed=50
var horizontal_speed=600
func _physics_process(delta):
direction.x=Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
direction.x*=horizontal_speed
if(is_on_floor()):
if(Input.is_action_just_pressed("ui_up")):
direction.y=jump_speed
## Movement ##
direction.y += gravity_speed * delta + descend_speed * Input.get_action_strength("ui_down")
direction=move_and_slide(direction,Vector2.UP)
but it only works in pushing the player back when the player is not given any user input and the body is static, but when the player is given user input it does not seem to work, instead it starts moving faster (I'm guessing since move_and_slide
is being called twice?)
So is there any fix or alternate solution which involves the same approach of not having to change the player script (much) and instead hands the movement alteration to the Area2D?