![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | infinity10004 |
during the development a rpg style game I came across a issue in the player script when we press hold a button the player moves a tile from its initial position, while I was trying to add turning to the character script I tried too simply make the character turn if we just pressed a button but instead it would just move normally it looks like godot isn’t able to tell the difference between an is_action_just_pressed and is_action_pressed any suggestions how i can fix it or an alternative to it
here is the code that i have used
extends KinematicBody2D
export var walk_speed = 4.0
const tile_size = 16
var velocity = Vector2()
var initial_position = Vector2(0,0)
var input_direction = Vector2(0,0)
var is_moving = false
var percent_moved_to_next_tile = 0.0
var can_move = true
var has_running_shoes = true
var is_running = false
var is_turning = false
onready var anim_player = $AnimatedSprite
var current_direction
func _ready():
initial_position = position
velocity = position
func _physics_process(delta):
if Input.is_action_pressed("decline") and can_move and has_running_shoes:
walk_speed = 8.0
is_running = true
else:
walk_speed = 4.0
is_running = false
if Input.is_action_just_pressed("D"):
is_turning = true
else:
is_turning = false
if input_direction.y == 1 and can_move:
if is_running == false:
anim_player.play("walk_down")
current_direction = "down"
elif is_running == true:
anim_player.play("run_down")
current_direction = "down"
elif input_direction.y == -1 and can_move:
if is_running == false:
anim_player.play("walk_up")
current_direction = "up"
elif is_running == true:
anim_player.play("run_up")
current_direction = "up"
else:
if current_direction == str("down") and can_move:
anim_player.play("idle_1")
elif current_direction == str("up") and can_move:
anim_player.play("idle_3")
if input_direction.x == 1 and can_move and is_turning == false:
if is_running == false:
anim_player.play("walk_right")
current_direction = "right"
elif is_running == true:
anim_player.play("run_right")
current_direction = "right"
elif input_direction.x == -1 and can_move and is_turning == false:
if is_running == false:
anim_player.play("walk_left")
current_direction = "left"
elif is_running == true:
anim_player.play("run_left")
current_direction = "left"
else:
if current_direction == str("right") and can_move:
anim_player.play("idle_2")
elif current_direction == str("left") and can_move:
anim_player.play("idle_4")
if is_moving == false:
process_player_input()
elif input_direction != Vector2.ZERO:
move(delta)
else:
is_moving = false
func process_player_input():
if input_direction.y == 0:
input_direction.x = int(Input.is_action_pressed("D")) - int(Input.is_action_pressed("A"))
if input_direction.x == 0:
input_direction.y = int(Input.is_action_pressed("S")) - int(Input.is_action_pressed("W"))
if input_direction != Vector2.ZERO:
initial_position = position
is_moving = true
func move(delta):
percent_moved_to_next_tile += walk_speed * delta
if percent_moved_to_next_tile >= 1:
position = initial_position + (tile_size * input_direction)
percent_moved_to_next_tile = 0.0
is_moving = false
else:
position = initial_position + (tile_size * input_direction * percent_moved_to_next_tile)
so any idea how i can implement turning mechanism if yes the please reply