From 15b8a2049358437b85352e8f4030a30258b82ed8 Mon Sep 17 00:00:00 2001 From: karl Date: Thu, 24 Jun 2021 13:34:25 +0200 Subject: [PATCH] Add joystick input --- DeadzoneInput.gd | 40 ++++++++++++++++++++++++++++++++++++++++ Player.gd | 16 +++++----------- World.tscn | 4 ++-- project.godot | 24 ++++++++++++++++++++---- 4 files changed, 67 insertions(+), 17 deletions(-) create mode 100644 DeadzoneInput.gd diff --git a/DeadzoneInput.gd b/DeadzoneInput.gd new file mode 100644 index 0000000..ab15139 --- /dev/null +++ b/DeadzoneInput.gd @@ -0,0 +1,40 @@ +extends Object +class_name DeadzoneInput + + +static func get_input(type, outer_deadzone, inner_deadzone, min_length = 0.0, normalized = true): + var input = Vector2(Input.get_action_strength(type + "_up") - + Input.get_action_strength(type + "_down"), + Input.get_action_strength(type + "_right") - + Input.get_action_strength(type + "_left")) + + # Remove signs to reduce the number of cases + var signs = Vector2(sign(input.x), sign(input.y)) + input = Vector2(abs(input.x), abs(input.y)) + + if input.length() < min_length: + return Vector2.ZERO + + # Deazones for each axis + if input.x > outer_deadzone: + input.x = 1.0 + elif input.x < inner_deadzone: + input.x = 0.0 + else: + input.x = inverse_lerp(inner_deadzone, outer_deadzone, input.x) + + if input.y > outer_deadzone: + input.y = 1.0 + elif input.y < inner_deadzone: + input.y = 0.0 + else: + input.y = inverse_lerp(inner_deadzone, outer_deadzone, input.y) + + # Re-apply signs + input *= signs + + # Limit at length 1 + if normalized and input.length() > 1.0: + input /= input.length() + + return input diff --git a/Player.gd b/Player.gd index 1a1de58..03bfafe 100644 --- a/Player.gd +++ b/Player.gd @@ -111,15 +111,11 @@ func _physics_process(delta): var move_velocity := Vector3.ZERO var move_acceleration := Vector3.ZERO - # Movement and rotation - if Input.is_action_pressed("move_up"): - move_acceleration.z -= move_accel - if Input.is_action_pressed("move_down"): - move_acceleration.z += move_accel - if Input.is_action_pressed("move_left"): - rotate(transform.basis.y, delta * rotate_speed) - if Input.is_action_pressed("move_right"): - rotate(transform.basis.y, -delta * rotate_speed) + # Apply input + var input = DeadzoneInput.get_input("move", 0.65, 0.2, 0.0, false) + print(input) + move_acceleration.z += input.x * -move_accel + rotate(transform.basis.y, delta * rotate_speed * -input.y) # Make movement local move_acceleration = transform.basis * move_acceleration @@ -127,8 +123,6 @@ func _physics_process(delta): # Get acceleration caused by gravity var gravity_acceleration = get_gravity_acceleration() - # FIXME: Consider setting the gravity_acceleration to 0 if on ground - # Apply both acceleration types apply_acceleration((move_acceleration + gravity_acceleration) * delta) diff --git a/World.tscn b/World.tscn index d29d95b..37e7f8c 100644 --- a/World.tscn +++ b/World.tscn @@ -15,7 +15,7 @@ [sub_resource type="CubeMesh" id=1] size = Vector3( 10, 0.5, 10 ) -[sub_resource type="ConcavePolygonShape" id=12] +[sub_resource type="ConcavePolygonShape" id=2] data = PoolVector3Array( -5, 0.25, 5, 5, 0.25, 5, -5, -0.25, 5, 5, 0.25, 5, 5, -0.25, 5, -5, -0.25, 5, 5, 0.25, -5, -5, 0.25, -5, 5, -0.25, -5, -5, 0.25, -5, -5, -0.25, -5, 5, -0.25, -5, 5, 0.25, 5, 5, 0.25, -5, 5, -0.25, 5, 5, 0.25, -5, 5, -0.25, -5, 5, -0.25, 5, -5, 0.25, -5, -5, 0.25, 5, -5, -0.25, -5, -5, 0.25, 5, -5, -0.25, 5, -5, -0.25, -5, 5, 0.25, 5, -5, 0.25, 5, 5, 0.25, -5, -5, 0.25, 5, -5, 0.25, -5, 5, 0.25, -5, -5, -0.25, 5, 5, -0.25, 5, -5, -0.25, -5, 5, -0.25, 5, 5, -0.25, -5, -5, -0.25, -5 ) [sub_resource type="PhysicsMaterial" id=3] @@ -118,7 +118,7 @@ mesh = SubResource( 1 ) material/0 = null [node name="CollisionShape" type="CollisionShape" parent="MovingPlatformPivot/MovingPlatform"] -shape = SubResource( 12 ) +shape = SubResource( 2 ) [node name="Planets" type="Spatial" parent="."] script = ExtResource( 2 ) diff --git a/project.godot b/project.godot index 6ae7d52..a711958 100644 --- a/project.godot +++ b/project.godot @@ -9,6 +9,11 @@ config_version=4 _global_script_classes=[ { +"base": "Object", +"class": "DeadzoneInput", +"language": "GDScript", +"path": "res://DeadzoneInput.gd" +}, { "base": "KinematicBody", "class": "MovingPlatform", "language": "GDScript", @@ -25,6 +30,7 @@ _global_script_classes=[ { "path": "res://Planets.gd" } ] _global_script_class_icons={ +"DeadzoneInput": "", "MovingPlatform": "", "Planet": "", "SolarSystem": "" @@ -63,28 +69,38 @@ ui_down={ ] } move_up={ -"deadzone": 0.5, +"deadzone": 0.0, "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":87,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":-1.0,"script":null) ] } move_down={ -"deadzone": 0.5, +"deadzone": 0.0, "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":83,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":1.0,"script":null) ] } move_left={ -"deadzone": 0.5, +"deadzone": 0.0, "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":65,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":-1.0,"script":null) ] } move_right={ -"deadzone": 0.5, +"deadzone": 0.0, "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":68,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":1.0,"script":null) ] } jump={ "deadzone": 0.5, "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":32,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":0,"pressure":0.0,"pressed":false,"script":null) + ] +} +movement_modifier={ +"deadzone": 0.5, +"events": [ Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":6,"pressure":0.0,"pressed":false,"script":null) ] }