├── FPS_controller_3.3 ├── FPS.gd └── FPS.tscn ├── FPS_controller_4.0 ├── FPS.gd └── FPS.tscn ├── FPS_controller_4.0_CSharp ├── Player.cs └── Player.tscn ├── LICENSE ├── README.md ├── TPS_controller_3.3 ├── TPS.gd └── TPS.tscn └── TPS_controller_4.0 ├── TPS.gd └── TPS.tscn /FPS_controller_3.3/FPS.gd: -------------------------------------------------------------------------------- 1 | extends KinematicBody 2 | 3 | var speed = 7 4 | const ACCEL_DEFAULT = 7 5 | const ACCEL_AIR = 1 6 | onready var accel = ACCEL_DEFAULT 7 | var gravity = 9.8 8 | var jump = 5 9 | 10 | var cam_accel = 40 11 | var mouse_sense = 0.1 12 | var snap 13 | 14 | var direction = Vector3() 15 | var velocity = Vector3() 16 | var gravity_vec = Vector3() 17 | var movement = Vector3() 18 | 19 | onready var head = $Head 20 | onready var camera = $Head/Camera 21 | 22 | func _ready(): 23 | #hides the cursor 24 | Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) 25 | 26 | func _input(event): 27 | #get mouse input for camera rotation 28 | if event is InputEventMouseMotion: 29 | rotate_y(deg2rad(-event.relative.x * mouse_sense)) 30 | head.rotate_x(deg2rad(-event.relative.y * mouse_sense)) 31 | head.rotation.x = clamp(head.rotation.x, deg2rad(-89), deg2rad(89)) 32 | 33 | func _process(delta): 34 | #camera physics interpolation to reduce physics jitter on high refresh-rate monitors 35 | if Engine.get_frames_per_second() > Engine.iterations_per_second: 36 | camera.set_as_toplevel(true) 37 | camera.global_transform.origin = camera.global_transform.origin.linear_interpolate(head.global_transform.origin, cam_accel * delta) 38 | camera.rotation.y = rotation.y 39 | camera.rotation.x = head.rotation.x 40 | else: 41 | camera.set_as_toplevel(false) 42 | camera.global_transform = head.global_transform 43 | 44 | func _physics_process(delta): 45 | #get keyboard input 46 | direction = Vector3.ZERO 47 | var h_rot = global_transform.basis.get_euler().y 48 | var f_input = Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward") 49 | var h_input = Input.get_action_strength("move_right") - Input.get_action_strength("move_left") 50 | direction = Vector3(h_input, 0, f_input).rotated(Vector3.UP, h_rot).normalized() 51 | 52 | #jumping and gravity 53 | if is_on_floor(): 54 | snap = -get_floor_normal() 55 | accel = ACCEL_DEFAULT 56 | gravity_vec = Vector3.ZERO 57 | else: 58 | snap = Vector3.DOWN 59 | accel = ACCEL_AIR 60 | gravity_vec += Vector3.DOWN * gravity * delta 61 | 62 | if Input.is_action_just_pressed("jump") and is_on_floor(): 63 | snap = Vector3.ZERO 64 | gravity_vec = Vector3.UP * jump 65 | 66 | #make it move 67 | velocity = velocity.linear_interpolate(direction * speed, accel * delta) 68 | movement = velocity + gravity_vec 69 | 70 | move_and_slide_with_snap(movement, snap, Vector3.UP) 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /FPS_controller_3.3/FPS.tscn: -------------------------------------------------------------------------------- 1 | [gd_scene load_steps=4 format=2] 2 | 3 | [ext_resource path="res://FPS_controller_3.3/FPS.gd" type="Script" id=1] 4 | 5 | [sub_resource type="CapsuleShape" id=1] 6 | radius = 0.5 7 | 8 | [sub_resource type="CapsuleMesh" id=2] 9 | radius = 0.5 10 | 11 | [node name="FPS" type="KinematicBody"] 12 | script = ExtResource( 1 ) 13 | 14 | [node name="CollisionShape" type="CollisionShape" parent="."] 15 | transform = Transform( 1, 0, 0, 0, -1.62921e-07, -1, 0, 1, -1.62921e-07, 0, 0, 0 ) 16 | shape = SubResource( 1 ) 17 | 18 | [node name="MeshInstance" type="MeshInstance" parent="."] 19 | transform = Transform( 1, 0, 0, 0, -1.62921e-07, -1, 0, 1, -1.62921e-07, 0, 0, 0 ) 20 | mesh = SubResource( 2 ) 21 | material/0 = null 22 | 23 | [node name="Head" type="Spatial" parent="."] 24 | transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, 0 ) 25 | 26 | [node name="Camera" type="Camera" parent="Head"] 27 | fov = 90.0 28 | -------------------------------------------------------------------------------- /FPS_controller_4.0/FPS.gd: -------------------------------------------------------------------------------- 1 | extends KinematicBody 2 | 3 | var speed = 7 4 | const ACCEL_DEFAULT = 7 5 | const ACCEL_AIR = 1 6 | @onready var accel = ACCEL_DEFAULT 7 | var gravity = 9.8 8 | var jump = 5 9 | 10 | var cam_accel = 40 11 | var mouse_sense = 0.1 12 | var snap 13 | 14 | var direction = Vector3() 15 | var velocity = Vector3() 16 | var gravity_vec = Vector3() 17 | var movement = Vector3() 18 | 19 | @onready var head = $Head 20 | @onready var camera = $Head/Camera 21 | 22 | func _ready(): 23 | #hides the cursor 24 | Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) 25 | 26 | func _input(event): 27 | #get mouse input for camera rotation 28 | if event is InputEventMouseMotion: 29 | rotate_y(deg2rad(-event.relative.x * mouse_sense)) 30 | head.rotate_x(deg2rad(-event.relative.y * mouse_sense)) 31 | head.rotation.x = clamp(head.rotation.x, deg2rad(-89), deg2rad(89)) 32 | 33 | func _process(delta): 34 | #camera physics interpolation to reduce physics jitter on high refresh-rate monitors 35 | if Engine.get_frames_per_second() > Engine.iterations_per_second: 36 | camera.set_as_toplevel(true) 37 | camera.global_transform.origin = camera.global_transform.origin.linear_interpolate(head.global_transform.origin, cam_accel * delta) 38 | camera.rotation.y = rotation.y 39 | camera.rotation.x = head.rotation.x 40 | else: 41 | camera.set_as_toplevel(false) 42 | camera.global_transform = head.global_transform 43 | 44 | func _physics_process(delta): 45 | #get keyboard input 46 | direction = Vector3.ZERO 47 | var h_rot = global_transform.basis.get_euler().y 48 | var f_input = Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward") 49 | var h_input = Input.get_action_strength("move_right") - Input.get_action_strength("move_left") 50 | direction = Vector3(h_input, 0, f_input).rotated(Vector3.UP, h_rot).normalized() 51 | 52 | #jumping and gravity 53 | if is_on_floor(): 54 | snap = -get_floor_normal() 55 | accel = ACCEL_DEFAULT 56 | gravity_vec = Vector3.ZERO 57 | else: 58 | snap = Vector3.DOWN 59 | accel = ACCEL_AIR 60 | gravity_vec += Vector3.DOWN * gravity * delta 61 | 62 | if Input.is_action_just_pressed("jump") and is_on_floor(): 63 | snap = Vector3.ZERO 64 | gravity_vec = Vector3.UP * jump 65 | 66 | #make it move 67 | velocity = velocity.linear_interpolate(direction * speed, accel * delta) 68 | movement = velocity + gravity_vec 69 | 70 | move_and_slide_with_snap(movement, snap, Vector3.UP) 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /FPS_controller_4.0/FPS.tscn: -------------------------------------------------------------------------------- 1 | [gd_scene load_steps=4 format=2] 2 | 3 | [ext_resource path="res://FPS_controller_4.0/FPS.gd" type="Script" id=1] 4 | 5 | [sub_resource type="CapsuleShape" id=1] 6 | radius = 0.5 7 | 8 | [sub_resource type="CapsuleMesh" id=2] 9 | radius = 0.5 10 | 11 | [node name="FPS" type="KinematicBody"] 12 | script = ExtResource( 1 ) 13 | 14 | [node name="CollisionShape" type="CollisionShape" parent="."] 15 | transform = Transform( 1, 0, 0, 0, -1.62921e-07, -1, 0, 1, -1.62921e-07, 0, 0, 0 ) 16 | shape = SubResource( 1 ) 17 | 18 | [node name="MeshInstance" type="MeshInstance" parent="."] 19 | transform = Transform( 1, 0, 0, 0, -1.62921e-07, -1, 0, 1, -1.62921e-07, 0, 0, 0 ) 20 | mesh = SubResource( 2 ) 21 | material/0 = null 22 | 23 | [node name="Head" type="Spatial" parent="."] 24 | transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, 0 ) 25 | 26 | [node name="Camera" type="Camera" parent="Head"] 27 | fov = 90.0 28 | -------------------------------------------------------------------------------- /FPS_controller_4.0_CSharp/Player.cs: -------------------------------------------------------------------------------- 1 | using Godot; 2 | using System.Collections.Generic; 3 | 4 | public class Player : KinematicBody 5 | { 6 | Dictionary accel_type = new Dictionary(); 7 | 8 | Spatial head; 9 | Camera camera; 10 | 11 | float speed = 7f; 12 | float gravity = 9.8f; 13 | float jump = 5f; 14 | float cam_accel = 40f; 15 | float mouse_sense = 0.1f; 16 | 17 | Vector3 snap; 18 | Vector3 direction = new Vector3(); 19 | Vector3 velocity = new Vector3(); 20 | Vector3 gravity_vec = new Vector3(); 21 | Vector3 movement = new Vector3(); 22 | 23 | int accel; 24 | 25 | public override void _Ready() 26 | { 27 | accel_type.Add("default", 7); 28 | accel_type.Add("air", 1); 29 | accel = accel_type["default"]; 30 | head = GetNode("Head"); 31 | camera = GetNode("Head").GetChild(0); 32 | 33 | Input.SetMouseMode(Input.MouseMode.Captured); 34 | } 35 | 36 | public override void _Input(InputEvent @event) 37 | { 38 | if (@event is InputEventMouseMotion mouseMotion) { 39 | RotateY(Mathf.Deg2Rad(-mouseMotion.Relative.x * mouse_sense)); 40 | head.RotateX(Mathf.Deg2Rad(-mouseMotion.Relative.y * mouse_sense)); 41 | Vector3 rotDeg = head.RotationDegrees; 42 | rotDeg.x = Mathf.Clamp(rotDeg.x, -89f, 89f); 43 | head.RotationDegrees = rotDeg; 44 | } 45 | } 46 | 47 | public override void _Process(float delta) 48 | { 49 | if (Engine.GetFramesPerSecond() > Engine.IterationsPerSecond) { 50 | camera.SetAsToplevel(true); 51 | 52 | Vector3 Gtrans = head.GlobalTransform.origin; 53 | 54 | 55 | var cameraGT = camera.GlobalTransform; 56 | cameraGT.origin = camera.GlobalTransform.origin.LinearInterpolate(Gtrans, cam_accel * delta); 57 | camera.GlobalTransform = camGT; 58 | 59 | Vector3 camRot = camera.Rotation; 60 | camRot.y = Rotation.y; 61 | camRot.x = head.Rotation.x; 62 | camera.Rotation = camRot; 63 | } else { 64 | camera.SetAsToplevel(false); 65 | camera.GlobalTransform = head.GlobalTransform; 66 | } 67 | } 68 | 69 | public override void _PhysicsProcess(float delta) 70 | { 71 | direction = Vector3.Zero; 72 | var h_rot = GlobalTransform.basis.GetEuler().y; 73 | var f_input = Input.GetActionStrength("move_backward") - Input.GetActionStrength("move_forward"); 74 | var h_input = Input.GetActionStrength("move_right") - Input.GetActionStrength("move_left"); 75 | direction = new Vector3(h_input, 0, f_input).Rotated(Vector3.Up, h_rot).Normalized(); 76 | 77 | if (IsOnFloor()) { 78 | snap = -GetFloorNormal(); 79 | accel = accel_type["default"]; 80 | gravity_vec = Vector3.Zero; 81 | } else { 82 | snap = Vector3.Down; 83 | accel = accel_type["air"]; 84 | gravity_vec += Vector3.Down * gravity * delta; 85 | } 86 | 87 | if (Input.IsActionJustPressed("jump") && IsOnFloor()) { 88 | snap = Vector3.Zero; 89 | gravity_vec = Vector3.Up * jump; 90 | } 91 | 92 | velocity = velocity.LinearInterpolate(direction * speed, accel * delta); 93 | movement = velocity + gravity_vec; 94 | 95 | MoveAndSlideWithSnap(movement, snap, Vector3.Up); 96 | 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /FPS_controller_4.0_CSharp/Player.tscn: -------------------------------------------------------------------------------- 1 | [gd_scene load_steps=5 format=2] 2 | 3 | [ext_resource path="res://scripts/Player.cs" type="Script" id=1] 4 | 5 | [sub_resource type="CapsuleMesh" id=1] 6 | mid_height = 3.0 7 | 8 | [sub_resource type="CapsuleShape" id=2] 9 | height = 3.0 10 | 11 | [sub_resource type="CylinderShape" id=3] 12 | 13 | [node name="Player" type="KinematicBody"] 14 | script = ExtResource( 1 ) 15 | 16 | [node name="MeshInstance" type="MeshInstance" parent="."] 17 | transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 0 ) 18 | mesh = SubResource( 1 ) 19 | material/0 = null 20 | 21 | [node name="CollisionShape" type="CollisionShape" parent="."] 22 | transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 0 ) 23 | shape = SubResource( 2 ) 24 | 25 | [node name="Foot" type="CollisionShape" parent="."] 26 | transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1.6, 0 ) 27 | shape = SubResource( 3 ) 28 | 29 | [node name="Head" type="Spatial" parent="."] 30 | transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.6, 0 ) 31 | 32 | [node name="Camera" type="Camera" parent="Head"] 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Garbaj 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # godot_updated_fps_controller 2 | Updated basic fps and tps controllers for Godot 3.x and 4.0. 3 | 4 | Features include: 5 | 6 | -basic movement and jumping 7 | 8 | -physics interpolation to reduce jitter on high refresh rate monitors 9 | 10 | -solves all weird slope sliding/climbing issues 11 | 12 | 13 | Suggestions: 14 | 15 | -attach FPS.gd to the main node (kinematic body) of FPS.tscn 16 | 17 | -attach TPS.gd to the main node (kinematic body) of TPS.tscn 18 | 19 | -names for wasd keybindings are "move_forward" "move_backward" "move_left" and "move_right" 20 | 21 | -this is just a character controller, I assume you already have a game world set up to test it out on 22 | -------------------------------------------------------------------------------- /TPS_controller_3.3/TPS.gd: -------------------------------------------------------------------------------- 1 | extends KinematicBody 2 | 3 | var speed = 7 4 | const ACCEL_DEFAULT = 10 5 | const ACCEL_AIR = 1 6 | onready var accel = ACCEL_DEFAULT 7 | var gravity = 9.8 8 | var jump = 5 9 | 10 | var cam_accel = 40 11 | var mouse_sense = 0.1 12 | var snap 13 | 14 | var angular_velocity = 30 15 | 16 | var direction = Vector3() 17 | var velocity = Vector3() 18 | var gravity_vec = Vector3() 19 | var movement = Vector3() 20 | 21 | onready var head = $Head 22 | onready var campivot = $Head/CamPivot 23 | onready var mesh = $MeshInstance 24 | 25 | func _ready(): 26 | #hides the cursor 27 | Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) 28 | 29 | #mesh no longer inherits rotation of parent, allowing it to rotate freely 30 | mesh.set_as_toplevel(true) 31 | 32 | func _input(event): 33 | #get mouse input for camera rotation 34 | if event is InputEventMouseMotion: 35 | rotate_y(deg2rad(-event.relative.x * mouse_sense)) 36 | head.rotate_x(deg2rad(-event.relative.y * mouse_sense)) 37 | head.rotation.x = clamp(head.rotation.x, deg2rad(-89), deg2rad(89)) 38 | 39 | func _process(delta): 40 | #physics interpolation to reduce jitter on high refresh-rate monitors 41 | var fps = Engine.get_frames_per_second() 42 | if fps > Engine.iterations_per_second: 43 | campivot.set_as_toplevel(true) 44 | campivot.global_transform.origin = campivot.global_transform.origin.linear_interpolate(head.global_transform.origin, cam_accel * delta) 45 | campivot.rotation.y = rotation.y 46 | campivot.rotation.x = head.rotation.x 47 | mesh.global_transform.origin = mesh.global_transform.origin.linear_interpolate(global_transform.origin, cam_accel * delta) 48 | else: 49 | campivot.set_as_toplevel(false) 50 | campivot.global_transform = head.global_transform 51 | mesh.global_transform.origin = global_transform.origin 52 | 53 | #turns body in the direction of movement 54 | if direction != Vector3.ZERO: 55 | mesh.rotation.y = lerp_angle(mesh.rotation.y, atan2(-direction.x, -direction.z), angular_velocity * delta) 56 | 57 | func _physics_process(delta): 58 | #get keyboard input 59 | direction = Vector3.ZERO 60 | var h_rot = global_transform.basis.get_euler().y 61 | var f_input = Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward") 62 | var h_input = Input.get_action_strength("move_right") - Input.get_action_strength("move_left") 63 | direction = Vector3(h_input, 0, f_input).rotated(Vector3.UP, h_rot).normalized() 64 | 65 | #jumping and gravity 66 | if is_on_floor(): 67 | snap = -get_floor_normal() 68 | accel = ACCEL_DEFAULT 69 | gravity_vec = Vector3.ZERO 70 | else: 71 | snap = Vector3.DOWN 72 | accel = ACCEL_AIR 73 | gravity_vec += Vector3.DOWN * gravity * delta 74 | 75 | if Input.is_action_just_pressed("jump") and is_on_floor(): 76 | snap = Vector3.ZERO 77 | gravity_vec = Vector3.UP * jump 78 | 79 | #make it move 80 | velocity = velocity.linear_interpolate(direction * speed, accel * delta) 81 | movement = velocity + gravity_vec 82 | 83 | move_and_slide_with_snap(movement, snap, Vector3.UP) 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /TPS_controller_3.3/TPS.tscn: -------------------------------------------------------------------------------- 1 | [gd_scene load_steps=5 format=2] 2 | 3 | [ext_resource path="res://TPS_controller_3.3/TPS.gd" type="Script" id=1] 4 | 5 | [sub_resource type="CapsuleShape" id=1] 6 | radius = 0.5 7 | 8 | [sub_resource type="CapsuleMesh" id=2] 9 | radius = 0.5 10 | 11 | [sub_resource type="CubeMesh" id=3] 12 | size = Vector3( 0.4, 0.4, 0.4 ) 13 | 14 | [node name="TPS" type="KinematicBody"] 15 | collision_layer = 2 16 | script = ExtResource( 1 ) 17 | 18 | [node name="CollisionShape" type="CollisionShape" parent="."] 19 | transform = Transform( 1, 0, 0, 0, -1.62921e-07, -1, 0, 1, -1.62921e-07, 0, 0, 0 ) 20 | shape = SubResource( 1 ) 21 | 22 | [node name="MeshInstance" type="MeshInstance" parent="."] 23 | transform = Transform( 1, 0, 0, 0, -1.62921e-07, -1, 0, 1, -1.62921e-07, 0, 0, 0 ) 24 | mesh = SubResource( 2 ) 25 | material/0 = null 26 | 27 | [node name="MeshInstance" type="MeshInstance" parent="MeshInstance"] 28 | transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.5, -0.734567 ) 29 | mesh = SubResource( 3 ) 30 | material/0 = null 31 | 32 | [node name="Head" type="Spatial" parent="."] 33 | transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5, 0 ) 34 | 35 | [node name="CamPivot" type="Spatial" parent="Head"] 36 | 37 | [node name="ClippedCamera" type="ClippedCamera" parent="Head/CamPivot"] 38 | transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 4 ) 39 | fov = 90.0 40 | -------------------------------------------------------------------------------- /TPS_controller_4.0/TPS.gd: -------------------------------------------------------------------------------- 1 | extends KinematicBody 2 | 3 | var speed = 7 4 | const ACCEL_DEFAULT = 10 5 | const ACCEL_AIR = 1 6 | @onready var accel = ACCEL_DEFAULT 7 | var gravity = 9.8 8 | var jump = 5 9 | 10 | var cam_accel = 40 11 | var mouse_sense = 0.1 12 | var snap 13 | 14 | var angular_velocity = 30 15 | 16 | var direction = Vector3() 17 | var velocity = Vector3() 18 | var gravity_vec = Vector3() 19 | var movement = Vector3() 20 | 21 | @onready var head = $Head 22 | @onready var campivot = $Head/CamPivot 23 | @onready var mesh = $MeshInstance 24 | 25 | func _ready(): 26 | #hides the cursor 27 | Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) 28 | 29 | #mesh no longer inherits rotation of parent, allowing it to rotate freely 30 | mesh.set_as_toplevel(true) 31 | 32 | func _input(event): 33 | #get mouse input for camera rotation 34 | if event is InputEventMouseMotion: 35 | rotate_y(deg2rad(-event.relative.x * mouse_sense)) 36 | head.rotate_x(deg2rad(-event.relative.y * mouse_sense)) 37 | head.rotation.x = clamp(head.rotation.x, deg2rad(-89), deg2rad(89)) 38 | 39 | func _process(delta): 40 | #physics interpolation to reduce jitter on high refresh-rate monitors 41 | var fps = Engine.get_frames_per_second() 42 | if fps > Engine.iterations_per_second: 43 | campivot.set_as_toplevel(true) 44 | campivot.global_transform.origin = campivot.global_transform.origin.linear_interpolate(head.global_transform.origin, cam_accel * delta) 45 | campivot.rotation.y = rotation.y 46 | campivot.rotation.x = head.rotation.x 47 | mesh.global_transform.origin = mesh.global_transform.origin.linear_interpolate(global_transform.origin, cam_accel * delta) 48 | else: 49 | campivot.set_as_toplevel(false) 50 | campivot.global_transform = head.global_transform 51 | mesh.global_transform.origin = global_transform.origin 52 | 53 | #turns body in the direction of movement 54 | if direction != Vector3.ZERO: 55 | mesh.rotation.y = lerp_angle(mesh.rotation.y, atan2(-direction.x, -direction.z), angular_velocity * delta) 56 | 57 | func _physics_process(delta): 58 | #get keyboard input 59 | direction = Vector3.ZERO 60 | var h_rot = global_transform.basis.get_euler().y 61 | var f_input = Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward") 62 | var h_input = Input.get_action_strength("move_right") - Input.get_action_strength("move_left") 63 | direction = Vector3(h_input, 0, f_input).rotated(Vector3.UP, h_rot).normalized() 64 | 65 | #jumping and gravity 66 | if is_on_floor(): 67 | snap = -get_floor_normal() 68 | accel = ACCEL_DEFAULT 69 | gravity_vec = Vector3.ZERO 70 | else: 71 | snap = Vector3.DOWN 72 | accel = ACCEL_AIR 73 | gravity_vec += Vector3.DOWN * gravity * delta 74 | 75 | if Input.is_action_just_pressed("jump") and is_on_floor(): 76 | snap = Vector3.ZERO 77 | gravity_vec = Vector3.UP * jump 78 | 79 | #make it move 80 | velocity = velocity.linear_interpolate(direction * speed, accel * delta) 81 | movement = velocity + gravity_vec 82 | 83 | move_and_slide_with_snap(movement, snap, Vector3.UP) 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /TPS_controller_4.0/TPS.tscn: -------------------------------------------------------------------------------- 1 | [gd_scene load_steps=5 format=2] 2 | 3 | [ext_resource path="res://TPS_controller_4.0/TPS.gd" type="Script" id=1] 4 | 5 | [sub_resource type="CapsuleShape" id=1] 6 | radius = 0.5 7 | 8 | [sub_resource type="CapsuleMesh" id=2] 9 | radius = 0.5 10 | 11 | [sub_resource type="CubeMesh" id=3] 12 | size = Vector3( 0.4, 0.4, 0.4 ) 13 | 14 | [node name="TPS" type="KinematicBody"] 15 | collision_layer = 2 16 | script = ExtResource( 1 ) 17 | 18 | [node name="CollisionShape" type="CollisionShape" parent="."] 19 | transform = Transform( 1, 0, 0, 0, -1.62921e-07, -1, 0, 1, -1.62921e-07, 0, 0, 0 ) 20 | shape = SubResource( 1 ) 21 | 22 | [node name="MeshInstance" type="MeshInstance" parent="."] 23 | transform = Transform( 1, 0, 0, 0, -1.62921e-07, -1, 0, 1, -1.62921e-07, 0, 0, 0 ) 24 | mesh = SubResource( 2 ) 25 | material/0 = null 26 | 27 | [node name="MeshInstance" type="MeshInstance" parent="MeshInstance"] 28 | transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.5, -0.734567 ) 29 | mesh = SubResource( 3 ) 30 | material/0 = null 31 | 32 | [node name="Head" type="Spatial" parent="."] 33 | transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5, 0 ) 34 | 35 | [node name="CamPivot" type="Spatial" parent="Head"] 36 | 37 | [node name="ClippedCamera" type="ClippedCamera" parent="Head/CamPivot"] 38 | transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 4 ) 39 | fov = 90.0 40 | --------------------------------------------------------------------------------