basic-move-and-dodge-game

[LEARNING] basic godot game based off the tutorial by borncg on youtube
git clone git://git.figbert.com/basic-move-and-dodge-game.git
Log | Files | Refs | README

commit a141217ad8c44004ea807f7f724fbdf600777123
parent 54918b639e845b67f8311440ccc0dd17acfaafc3
Author: FIGBERT <figbert@figbert.com>
Date:   Mon, 23 Nov 2020 12:21:19 -0800

Update code for second section of Godot tutorial

Diffstat:
MLevel.tscn | 38+++++++++++++-------------------------
Aassets/Character.tscn | 14++++++++++++++
Ascripts/Character.gd | 32++++++++++++++++++++++++++++++++
3 files changed, 59 insertions(+), 25 deletions(-)

diff --git a/Level.tscn b/Level.tscn @@ -1,4 +1,7 @@ -[gd_scene load_steps=5 format=2] +[gd_scene load_steps=7 format=2] + +[ext_resource path="res://assets/Character.tscn" type="PackedScene" id=1] +[ext_resource path="res://scripts/Character.gd" type="Script" id=2] [sub_resource type="BoxShape" id=1] @@ -12,33 +15,14 @@ size = Vector3( 30, 2, 30 ) [node name="Level" type="Spatial"] -[node name="Box1" type="RigidBody" parent="."] -transform = Transform( 0.364682, -0.736898, 0.5692, -0.223911, 0.523958, 0.821786, -0.903809, -0.427141, 0.0260788, 0, 4.84117, 0 ) - -[node name="CollisionShape" type="CollisionShape" parent="Box1"] -shape = SubResource( 1 ) - -[node name="MeshInstance" type="MeshInstance" parent="Box1"] -mesh = SubResource( 2 ) -material/0 = null - -[node name="Box2" type="RigidBody" parent="."] -transform = Transform( 0.513511, 0.857976, 0.0135868, -0.252411, 0.166167, -0.953245, -0.820119, 0.486072, 0.301891, 0, 7.81481, 0 ) +[node name="Box" type="RigidBody" parent="."] +transform = Transform( 0.364682, -0.736898, 0.5692, -0.223911, 0.523958, 0.821786, -0.903809, -0.427141, 0.0260788, 0, 4.84117, -6.20138 ) -[node name="CollisionShape" type="CollisionShape" parent="Box2"] +[node name="CollisionShape" type="CollisionShape" parent="Box"] shape = SubResource( 1 ) -[node name="MeshInstance" type="MeshInstance" parent="Box2"] -mesh = SubResource( 2 ) -material/0 = null - -[node name="Box3" type="RigidBody" parent="."] -transform = Transform( -0.471122, 0.753256, -0.458966, -0.570719, -0.657047, -0.492513, -0.67255, 0.0299074, 0.739447, 0, 11.3101, 0 ) - -[node name="CollisionShape" type="CollisionShape" parent="Box3"] -shape = SubResource( 1 ) - -[node name="MeshInstance" type="MeshInstance" parent="Box3"] +[node name="MeshInstance" type="MeshInstance" parent="Box"] +transform = Transform( 1, 5.96046e-08, -3.1665e-08, 0, 1, -1.58325e-08, 5.58794e-09, 2.79397e-09, 1, 0, 0, 0 ) mesh = SubResource( 2 ) material/0 = null @@ -54,3 +38,7 @@ material/0 = null [node name="Camera" type="Camera" parent="."] transform = Transform( 1, 0, 0, 0, 0.851398, 0.524521, 0, -0.524521, 0.851398, 0, 4.91959, 10.0583 ) + +[node name="Character" parent="." instance=ExtResource( 1 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0 ) +script = ExtResource( 2 ) diff --git a/assets/Character.tscn b/assets/Character.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=3 format=2] + +[sub_resource type="BoxShape" id=1] + +[sub_resource type="SphereMesh" id=2] + +[node name="Character" type="KinematicBody"] + +[node name="CollisionShape" type="CollisionShape" parent="."] +shape = SubResource( 1 ) + +[node name="MeshInstance" type="MeshInstance" parent="."] +mesh = SubResource( 2 ) +material/0 = null diff --git a/scripts/Character.gd b/scripts/Character.gd @@ -0,0 +1,32 @@ +extends KinematicBody + +var velocity = Vector3(0,0,0) + +func _ready(): + pass + +func _physics_process(delta): + if ( + Input.is_action_pressed("ui_right") + and Input.is_action_pressed("ui_left") + ): + velocity.x = 0 + elif Input.is_action_pressed("ui_right"): + velocity.x = 5 + elif Input.is_action_pressed("ui_left"): + velocity.x = -5 + else: + velocity.x = lerp(velocity.x, 0, 0.1) + + if ( + Input.is_action_pressed("ui_up") + and Input.is_action_pressed("ui_down") + ): + velocity.z = 0 + elif Input.is_action_pressed("ui_up"): + velocity.z = -5 + elif Input.is_action_pressed("ui_down"): + velocity.z = 5 + else: + velocity.z = lerp(velocity.z, 0, 0.1) + move_and_slide(velocity)