Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | iiell |
I believe I understand what is happening and I’m hopeful someone out there can come up with a solution to my predicament. It seems like it would be simple, but I have not been able to figure out a solution.
I have a Player scene with a function that sets the process, physics process, and input process to active or inactive. Then I am inactivating the Player scene using an Area2D in the Level scene. I am using a Timer in this example to set the Player scene back to active after a short duration. The problem is when the Player scene is set back to active, the Player cannot “walk”. Of course, this only happens if the Player scene is “sliding” when it becomes inactive. I imagine this is due to the condition “is_sliding”, and I have tried everything I can think of to return functionality to the Player without success. Also, it is worth noting that if you “slide” again, you can then walk. Here is the code for each scene.
Player Scene:
extends KinematicBody2D
var is_sliding = false
var can_slide = true
var speed = 100
var gravity = 50
var velocity = Vector2(0,0)
var direction = 0
func _ready():
pass # Replace with function body.
func _physics_process(delta):
### WALK ###
if Input.is_action_pressed("ui_right") and is_sliding == false:
velocity.x = speed
direction = 0
elif Input.is_action_pressed("ui_left") and is_sliding == false:
velocity.x = -speed
direction = 1
### SLIDE ###
if can_slide and Input.is_action_just_pressed("ui_accept") and direction == 0:
velocity.x = 1000
is_sliding = true
can_slide = false
$Timer.start()
if can_slide and Input.is_action_just_pressed("ui_accept") and direction == 1:
velocity.x = -1000
is_sliding = true
can_slide = false
$Timer.start()
if Input.is_action_just_released("ui_accept"):
is_sliding = false
velocity.y += gravity * delta
velocity = move_and_slide(velocity,Vector2.UP)
velocity.x = lerp(velocity.x,0,0.1)
### Inactivate Player ###
func _set_active(active):
set_process(active)
set_physics_process(active)
set_process_input(active)
func _on_Timer_timeout():
can_slide = true
Level Scene:
extends Node2D
func _ready():
pass
func _turn_player_active():
var player = get_tree().get_root().find_node("Player", true, false)
if player:
player._set_active(true)
func _on_Area2D_body_entered(_body):
var player = get_tree().get_root().find_node("Player", true, false)
if player:
player._set_active(false)
$ActivatePlayer.start()
func _on_ActivatePlayer_timeout():
_turn_player_active()
Here is an example project as well.
Test_Input_Action
Thank you!