Help needed with aiming script

Godot Version

Godot 4.x

Question

Hello, I’m writing a script for directional aiming for a game im working on, but for some reason it wont respond to diagonal inputs

extends Node2D

var rot_state = 0


func check_input():
	if Input.is_action_pressed("Right"):
		rot_state = 0
	elif Input.is_action_pressed("Right") and Input.is_action_pressed("Up"):
		rot_state = 1
	elif Input.is_action_pressed("Up"):
		rot_state = 2
	elif Input.is_action_pressed("Left") and Input.is_action_pressed("Up"):
		rot_state = 3
	elif Input.is_action_pressed("Left"):
		rot_state = 4
	else:
		pass

func rotate_node():
	if rot_state == 0:
		rotation = 0
	elif rot_state == 1:
		rotation = deg_to_rad(-45)
	elif rot_state == 2:
		rotation = deg_to_rad(-90)
	elif rot_state == 3:
		rotation = deg_to_rad(-135)
	elif rot_state == 4:
		rotation = deg_to_rad(-180)

func _physics_process(delta):
	check_input()
	rotate_node()
	if Input.is_action_just_pressed("Shoot"):
		pass
1 Like

This code is not optimal, it takes some weird detours, but changing the order in which you do the checks in your check_input function should work:

func check_input():
	if Input.is_action_pressed("Right") and Input.is_action_pressed("Up"):
		rot_state = 1
	elif Input.is_action_pressed("Left") and Input.is_action_pressed("Up"):
		rot_state = 3
	elif Input.is_action_pressed("Right"):
		rot_state = 0
	elif Input.is_action_pressed("Up"):
		rot_state = 2
	elif Input.is_action_pressed("Left"):
		rot_state = 4
	else:
		pass
1 Like

thanks so much :blush:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.