extends Node # exposes a property "input_xz" # Old implementation had the player stop if two keys were pressed that go opposite directions # It's nicer if the key pressed later overrides the first key # Controller input should work the same since it's impossible to press opposite directions # Unless you bind forward and backward to different joysticks like a maniac # Also supports analog jump, idk maybe you can bind jump to an analog trigger, mine never seem to work right on my controller "public" var input_xz: Vector2 = Vector2(0.0, 0.0) "public" var jump_intent: float = 0.0 var vertical_preference = Vector2(1.0, 1.0) var horizontal_preference = Vector2(1.0, 1.0) # If input direction suddently changes, wait this many # seconds before caring var timer_start: float = 0.1 var horizontal_sign: float = 0.0 var vertical_sign: float = 0.0 func update_preference(input: Vector2, old: Vector2) -> Vector2: if input.x > 0.0 and input.y == 0.0: return Vector2(0.0, 1.0) elif input.x == 0.0 and input.y > 0.0: return Vector2(1.0, 0.0) return old func apply_preference(input: Vector2, preference: Vector2) -> Vector2: if input.x > 0.0 and input.y > 0.0: return input * preference else: return input func process_lateral_motion(): var vertical = Vector2(Input.get_action_strength("player_forwards"), Input.get_action_strength("player_backwards")) var horizontal = Vector2(Input.get_action_strength("player_right"), Input.get_action_strength("player_left")) vertical_preference = update_preference(vertical, vertical_preference) horizontal_preference = update_preference(horizontal, horizontal_preference) vertical = apply_preference(vertical, vertical_preference) horizontal = apply_preference(horizontal, horizontal_preference) # Commented out code is KB specific but misbehaves when a controller is plugged in. # Not super important, might be easier to just delete var x = vertical.x - vertical.y """if vertical_sign != 0.0: if sign(x) != vertical_sign: x = 0.0 if $VerticalTimer.is_stopped(): $VerticalTimer.start(timer_start) else: $VerticalTimer.stop() else: vertical_sign = sign(x) $VerticalTimer.stop() """ var y = horizontal.x - horizontal.y """if horizontal_sign != 0.0: if sign(y) != horizontal_sign: y = 0.0 if $HorizontalTimer.is_stopped(): $HorizontalTimer.start(timer_start) else: $HorizontalTimer.stop() else: horizontal_sign = sign(y) $HorizontalTimer.stop() """ input_xz = Vector2(x, y).rotated(-$"..".camera().global_rotation.y - PI/2).normalized() func process_jump(): jump_intent = Input.get_action_strength("player_jump") func _input(_event): process_lateral_motion() process_jump() func _on_VerticalTimer_timeout(): #vertical_sign = 0.0 $VerticalTimer.stop() #process_lateral_motion() func _on_HorizontalTimer_timeout(): #horizontal_sign = 0.0 $HorizontalTimer.stop() #process_lateral_motion()