Skip to content

Commit

Permalink
Add sprinting/jumping, Fix camera responsiveness
Browse files Browse the repository at this point in the history
About the fix: Camera would sometimes get sent to negative infinity if the slope was too high. Also made camera smooth less when falling down stairs to look more natural.
  • Loading branch information
DeerTears authored May 26, 2022
1 parent 876eb1a commit 8b60414
Show file tree
Hide file tree
Showing 12 changed files with 636 additions and 173 deletions.
350 changes: 318 additions & 32 deletions Main.tscn

Large diffs are not rendered by default.

36 changes: 13 additions & 23 deletions Player.gd
Original file line number Diff line number Diff line change
@@ -1,42 +1,32 @@
extends Spatial

"""
Additional script to handle interacting-with and shooting physics bodies.
Optional script to handle shooting physics bodies and toggling fullscreen.
This template does not include damaging targets or calling functions on interactive elements.
This template does not include how to damage targets, just how to recognize them.
All player movement (the meat of this template) is on the KinematicBody child.
"""

onready var player_body: KinematicBody = $Body
onready var head_camera: Camera = $Body/HeadCamera
onready var cursor_ray: RayCast = $Body/HeadCamera/CursorRay
onready var shoot_ray: RayCast = $Body/HeadCamera/ShootRay

func _unhandled_input(event):
# Requires "shoot" and "interact" setup in Project Settings -> Input Map.
if event.is_action_pressed("interact"):
_interact(true)
if event.is_action_released("interact"):
_interact(false)
func _unhandled_input(event: InputEvent) -> void:
if player_body.can_handle_input and event.is_action_pressed("shoot"):
_shoot()
shoot()
if event.is_action_released("toggle_fullscreen"):
OS.window_fullscreen = not OS.window_fullscreen

## Can be called to pause recieving the player's inputs, without pausing physics or the SceneTree.
## Pauses the player's ability to recieve inputs. Doesn't pause physics or the SceneTree.
func set_movement_input(_true: bool) -> void:
player_body.can_handle_input = _true

# Private functions. Simple games shouldn't need to access these in other scripts. Advanced games
# should move these functions into a new child node/script to handle more complex interactions.

func _interact(is_pressed: bool) -> void:
if not cursor_ray.is_colliding():
return
var body = cursor_ray.get_collider()
print_debug(body) # replace with code to handle interactable bodies

func _shoot() -> void:
func shoot() -> void:
if shoot_ray.is_colliding():
var body = shoot_ray.get_collider()
var body: PhysicsBody = shoot_ray.get_collider()
if body is StaticBody:
return # or process shooting the world here
return # handle shooting the world
else:
print_debug(body) # replace with code to handle shootable bodies
print_debug(body) # handle shooting moving bodies
83 changes: 67 additions & 16 deletions Player.tscn
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
[gd_scene load_steps=6 format=2]
[gd_scene load_steps=7 format=2]

[ext_resource path="res://Player.gd" type="Script" id=1]
[ext_resource path="res://PlayerBody.gd" type="Script" id=2]
[ext_resource path="res://crosshair.png" type="Texture" id=3]

[sub_resource type="RayShape" id=1]
length = 3.0

[sub_resource type="CylinderShape" id=2]
radius = 0.9
radius = 1.1
height = 1.0

[sub_resource type="CylinderMesh" id=3]
height = 3.0
top_radius = 1.1
bottom_radius = 1.1
height = 3.5
radial_segments = 32
rings = 1

[node name="Player" type="Spatial"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1 )
script = ExtResource( 1 )

[node name="Body" type="KinematicBody" parent="."]
Expand All @@ -22,39 +28,84 @@ collision_layer = 2
script = ExtResource( 2 )

[node name="RayShape" type="CollisionShape" parent="Body"]
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 0 )
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 1 )
shape = SubResource( 1 )

[node name="CylinderShape" type="CollisionShape" parent="Body"]
[node name="RayShape2" type="CollisionShape" parent="Body"]
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, -1 )
shape = SubResource( 1 )

[node name="RayShape3" type="CollisionShape" parent="Body"]
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, -1, 0, 0 )
shape = SubResource( 1 )

[node name="RayShape4" type="CollisionShape" parent="Body"]
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 1, 0, 0 )
shape = SubResource( 1 )

[node name="RayShape5" type="CollisionShape" parent="Body"]
transform = Transform( 0.707107, -0.707107, 3.09086e-08, 0, -4.37114e-08, -1, 0.707107, 0.707107, -3.09086e-08, -0.707107, 0, 0.707107 )
shape = SubResource( 1 )

[node name="RayShape6" type="CollisionShape" parent="Body"]
transform = Transform( 0.707107, -0.707107, 3.09086e-08, 0, -4.37114e-08, -1, 0.707107, 0.707107, -3.09086e-08, 0.707107, 0, -0.707107 )
shape = SubResource( 1 )

[node name="RayShape7" type="CollisionShape" parent="Body"]
transform = Transform( 0.707107, -0.707107, 3.09086e-08, 0, -4.37114e-08, -1, 0.707107, 0.707107, -3.09086e-08, -0.707107, 0, -0.707107 )
shape = SubResource( 1 )

[node name="RayShape8" type="CollisionShape" parent="Body"]
transform = Transform( 0.707107, -0.707107, 3.09086e-08, 0, -4.37114e-08, -1, 0.707107, 0.707107, -3.09086e-08, 0.707107, 0, 0.707107 )
shape = SubResource( 1 )

[node name="CylinderHead" type="CollisionShape" parent="Body"]
shape = SubResource( 2 )

[node name="HeadCamera" type="Camera" parent="Body"]
current = true
fov = 90.0
far = 90.0

[node name="CursorRay" type="RayCast" parent="Body/HeadCamera"]
enabled = true
cast_to = Vector3( 0, 0, -3 )
collision_mask = 4
collide_with_areas = true
[node name="RemoteTransform" type="RemoteTransform" parent="Body/HeadCamera"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1.25, 0 )
remote_path = NodePath("../../../PlayerModel")
update_rotation = false
update_scale = false

[node name="ShootRay" type="RayCast" parent="Body/HeadCamera"]
enabled = true
cast_to = Vector3( 0, 0, -20 )
collision_mask = 13
collide_with_areas = true

[node name="RemoteTransform" type="RemoteTransform" parent="Body/HeadCamera"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1.5, 0 )
remote_path = NodePath("../../../PlayerModel")
update_rotation = false
update_scale = false
[node name="CursorRay" type="RayCast" parent="Body/HeadCamera"]
enabled = true
cast_to = Vector3( 0, 0, -3 )
collision_mask = 4
collide_with_areas = true

[node name="CameraTargetPosition" type="Position3D" parent="Body"]

[node name="PlayerModel" type="MeshInstance" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5, 0 )
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.75, 0 )
visible = false
mesh = SubResource( 3 )
skeleton = NodePath("../Body")
material/0 = null

[node name="CanvasLayer" type="CanvasLayer" parent="."]

[node name="CenterContainer" type="CenterContainer" parent="CanvasLayer"]
anchor_right = 1.0
anchor_bottom = 1.0
mouse_filter = 2

[node name="TextureRect" type="TextureRect" parent="CanvasLayer/CenterContainer"]
margin_left = 604.0
margin_top = 324.0
margin_right = 676.0
margin_bottom = 396.0
mouse_filter = 2
texture = ExtResource( 3 )
stretch_mode = 3
Loading

0 comments on commit 8b60414

Please sign in to comment.