From d80caa306f97d146fc1aa171a0de5eab1d151a58 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 25 Nov 2023 13:49:33 +0100 Subject: [PATCH] Move --- Player.gd | 54 ++++++++++++++++++++++++++++++++------------------- Root.tscn | 17 +++++++++++----- project.godot | 32 ++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 25 deletions(-) diff --git a/Player.gd b/Player.gd index 7d1d981..9a90895 100644 --- a/Player.gd +++ b/Player.gd @@ -2,31 +2,45 @@ extends CharacterBody3D class_name Player -const SPEED = 5.0 -const JUMP_VELOCITY = 4.5 +@export var input_accel: float = 1.0 +@onready var current_input_accel = input_accel + +@onready var gravity_accel: Vector3 = ProjectSettings.get_setting("physics/3d/default_gravity") * ProjectSettings.get_setting("physics/3d/default_gravity_vector") +@export var jump_impulse: Vector3 = Vector3.UP * 100.0; + + +@export var max_jumps = 1 +@onready var current_jumps = max_jumps + + +func refill_jumps(): + current_jumps = max_jumps + + +func do_jump(): + current_jumps -= 1 + velocity += jump_impulse -# Get the gravity from the project settings to be synced with RigidBody nodes. -var gravity = ProjectSettings.get_setting("physics/3d/default_gravity") func _physics_process(delta): - # Add the gravity. - if not is_on_floor(): - velocity.y -= gravity * delta + # Refilla salto + if is_on_floor(): + refill_jumps() + + # Rileva salto + if Input.is_action_just_pressed("jump") and current_jumps > 0: + do_jump() + + # Applica gravità + velocity += gravity_accel - # Handle Jump. - if Input.is_action_just_pressed("ui_accept") and is_on_floor(): - velocity.y = JUMP_VELOCITY + # Applica input + var input_dir = Input.get_vector("move_left", "move_right", "move_up", "move_down") + var input_change = input_dir * input_accel + velocity.x += input_change.x + velocity.z += input_change.y - # Get the input direction and handle the movement/deceleration. - # As good practice, you should replace UI actions with custom gameplay actions. - var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") - var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() - if direction: - velocity.x = direction.x * SPEED - velocity.z = direction.z * SPEED - else: - velocity.x = move_toward(velocity.x, 0, SPEED) - velocity.z = move_toward(velocity.z, 0, SPEED) + print(velocity) move_and_slide() diff --git a/Root.tscn b/Root.tscn index 07de021..32f72dc 100644 --- a/Root.tscn +++ b/Root.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=9 format=3 uid="uid://4whij2hmts0t"] +[gd_scene load_steps=10 format=3 uid="uid://4whij2hmts0t"] [ext_resource type="Script" path="res://Player.gd" id="1_wt4gx"] [ext_resource type="Material" uid="uid://dondfiavgo2ws" path="res://Unshaded.material" id="2_upp15"] @@ -15,9 +15,16 @@ size = Vector3(1, 0.1, 3) material = ExtResource("2_upp15") size = Vector3(1, 0.1, 3) -[sub_resource type="PlaneMesh" id="PlaneMesh_opk07"] +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_1au4d"] +shading_mode = 0 +albedo_color = Color(0, 0.541176, 0, 1) -[sub_resource type="WorldBoundaryShape3D" id="WorldBoundaryShape3D_tpbxa"] +[sub_resource type="PlaneMesh" id="PlaneMesh_opk07"] +material = SubResource("StandardMaterial3D_1au4d") +size = Vector2(200, 200) + +[sub_resource type="BoxShape3D" id="BoxShape3D_2bm1d"] +size = Vector3(200, 4, 200) [node name="Root" type="Node3D"] @@ -44,11 +51,11 @@ skeleton = NodePath("../..") [node name="Plane" type="StaticBody3D" parent="."] [node name="MeshInstance3D" type="MeshInstance3D" parent="Plane"] -transform = Transform3D(100, 0, 0, 0, 1, 0, 0, 0, 100, 0, 0, 0) mesh = SubResource("PlaneMesh_opk07") [node name="CollisionShape3D" type="CollisionShape3D" parent="Plane"] -shape = SubResource("WorldBoundaryShape3D_tpbxa") +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -2, 0) +shape = SubResource("BoxShape3D_2bm1d") [node name="Camera3D" type="Camera3D" parent="."] transform = Transform3D(1, 0, 0, 0, 0.955149, 0.296125, 0, -0.296125, 0.955149, 0, 2.66196, 3.33182) diff --git a/project.godot b/project.godot index da8c83a..89ac43a 100644 --- a/project.godot +++ b/project.godot @@ -14,3 +14,35 @@ config/name="cisio" run/main_scene="res://Root.tscn" config/features=PackedStringArray("4.1", "Forward Plus") config/icon="res://icon.svg" + +[input] + +jump={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":32,"key_label":0,"unicode":32,"echo":false,"script":null) +] +} +move_up={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":119,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194320,"key_label":0,"unicode":0,"echo":false,"script":null) +] +} +move_down={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194322,"key_label":0,"unicode":0,"echo":false,"script":null) +] +} +move_left={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194319,"key_label":0,"unicode":0,"echo":false,"script":null) +] +} +move_right={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"echo":false,"script":null) +] +}