Browse Source

Fix players unable to jump when between two walls

master
Luna 7 years ago
parent
commit
f00849253a
2 changed files with 25 additions and 14 deletions
  1. +13
    -11
      scenes/player.tscn
  2. +12
    -3
      scripts/player.gd

+ 13
- 11
scenes/player.tscn View File

@ -10,7 +10,7 @@ height = 0.5
[sub_resource type="RayShape" id=2] [sub_resource type="RayShape" id=2]
length = 0.3
length = 0.1
_sections_unfolded = [ "Resource" ] _sections_unfolded = [ "Resource" ]
[sub_resource type="PrismMesh" id=3] [sub_resource type="PrismMesh" id=3]
@ -29,7 +29,7 @@ radial_segments = 64
rings = 8 rings = 8
_sections_unfolded = [ "Resource" ] _sections_unfolded = [ "Resource" ]
[node name="RigidBody" type="RigidBody" groups=[
[node name="RigidBody" type="RigidBody" index="0" groups=[
"player", "player",
]] ]]
@ -44,8 +44,8 @@ bounce = 0.0
gravity_scale = 1.0 gravity_scale = 1.0
custom_integrator = false custom_integrator = false
continuous_cd = true continuous_cd = true
contacts_reported = 0
contact_monitor = false
contacts_reported = 1
contact_monitor = true
sleeping = false sleeping = false
can_sleep = false can_sleep = false
axis_lock_linear_x = false axis_lock_linear_x = false
@ -63,32 +63,34 @@ _sections_unfolded = [ "Angular", "Axis Lock", "Collision", "Linear", "Transform
[node name="Body" type="CollisionShape" parent="." index="0"] [node name="Body" type="CollisionShape" parent="." index="0"]
transform = Transform( 1, 0, 0, 0, -1.62921e-07, -1, 0, 1, -1.62921e-07, 0, 0.95, 0 )
transform = Transform( 1, 0, 0, 0, -1.62921e-07, -1, 0, 1, -1.62921e-07, 0, 0.75, 0 )
shape = SubResource( 1 ) shape = SubResource( 1 )
disabled = false disabled = false
_sections_unfolded = [ "Pause", "Transform", "Visibility" ] _sections_unfolded = [ "Pause", "Transform", "Visibility" ]
[node name="Leg" type="CollisionShape" parent="." index="1"] [node name="Leg" type="CollisionShape" parent="." index="1"]
transform = Transform( 1, 0, 0, 0, -1.62921e-07, -1, 0, 1, -1.62921e-07, 0.05, 0.35, 0 )
transform = Transform( 1, 0, 0, 0, -1.62921e-07, -1, 0, 1, -1.62921e-07, 0.05, 0.1, 0 )
shape = SubResource( 2 ) shape = SubResource( 2 )
disabled = false disabled = false
_sections_unfolded = [ "Transform", "Visibility" ] _sections_unfolded = [ "Transform", "Visibility" ]
[node name="Ray" type="RayCast" parent="." index="2"] [node name="Ray" type="RayCast" parent="." index="2"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.3, 0 )
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.15, 0 )
enabled = true enabled = true
exclude_parent = true exclude_parent = true
cast_to = Vector3( 0, -0.3, 0 )
cast_to = Vector3( 0, -0.2, 0 )
collision_mask = 1 collision_mask = 1
_sections_unfolded = [ "Transform", "Visibility" ] _sections_unfolded = [ "Transform", "Visibility" ]
[node name="Yaw" type="Spatial" parent="." index="3"] [node name="Yaw" type="Spatial" parent="." index="3"]
_sections_unfolded = [ "Transform" ]
[node name="Pitch" type="Spatial" parent="Yaw" index="0"] [node name="Pitch" type="Spatial" parent="Yaw" index="0"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.4, 0 )
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.19053, 0 )
_sections_unfolded = [ "Transform" ] _sections_unfolded = [ "Transform" ]
[node name="MeshInstance" type="MeshInstance" parent="Yaw/Pitch" index="0"] [node name="MeshInstance" type="MeshInstance" parent="Yaw/Pitch" index="0"]
@ -154,7 +156,7 @@ max_lines_visible = -1
[node name="TPCamera" type="Spatial" parent="." index="5"] [node name="TPCamera" type="Spatial" parent="." index="5"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.74343, 0 )
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.56913, 0 )
script = ExtResource( 2 ) script = ExtResource( 2 )
_sections_unfolded = [ "Transform" ] _sections_unfolded = [ "Transform" ]
cam = NodePath("Camera") cam = NodePath("Camera")
@ -187,7 +189,7 @@ _sections_unfolded = [ "Transform" ]
[node name="MeshInstance" type="MeshInstance" parent="." index="6"] [node name="MeshInstance" type="MeshInstance" parent="." index="6"]
transform = Transform( 1, 0, 0, 0, -1.62921e-07, -1, 0, 1, -1.62921e-07, 0, 0.787368, 0 )
transform = Transform( 1, 0, 0, 0, -1.62921e-07, -1, 0, 1, -1.62921e-07, 0, 0.567933, 0 )
layers = 1 layers = 1
material_override = null material_override = null
cast_shadow = 1 cast_shadow = 1


+ 12
- 3
scripts/player.gd View File

@ -6,7 +6,7 @@ var view_sensitivity = 0.25
# Walking speed and jumping height are defined later. # Walking speed and jumping height are defined later.
var walk_speed = 0.8 # Actually acceleration; m/s/s var walk_speed = 0.8 # Actually acceleration; m/s/s
var jump_speed = 1 # m/s
var jump_speed = 1.5 # m/s
var air_accel = .1 # m/s/s var air_accel = .1 # m/s/s
var floor_friction = 1-0.08 var floor_friction = 1-0.08
var air_friction = 1-0.03 var air_friction = 1-0.03
@ -152,7 +152,7 @@ func control_player(state):
direction = direction.normalized() direction = direction.normalized()
var ray = get_node("Ray") var ray = get_node("Ray")
if ray.is_colliding():
if get_colliding_bodies(): # We can navigate normally, we have a surface
var up = state.get_total_gravity().normalized() var up = state.get_total_gravity().normalized()
var normal = ray.get_collision_normal() var normal = ray.get_collision_normal()
var floor_velocity = Vector3() var floor_velocity = Vector3()
@ -166,7 +166,16 @@ func control_player(state):
state.set_linear_velocity(lin_v) state.set_linear_velocity(lin_v)
if Input.is_action_pressed("jump"): if Input.is_action_pressed("jump"):
state.apply_impulse(Vector3(), normal * jump_speed * get_mass())
# This may be kinda expensive but we only check while pressing jump so it's ok
# Detect jumpable
var jump_dot = 0.8 # If normal.dot(up) > jump_dot, we can jump
var jumpable = false
for i in range(state.get_contact_count()):
var n = state.get_contact_local_normal(0)
if n.dot(Vector3(0,1,0)) > jump_dot:
jumpable = true
if jumpable:
state.apply_impulse(Vector3(), normal * jump_speed * get_mass())
else: else:
var accel = (1 + switch_charge * air_speed_build) * air_accel var accel = (1 + switch_charge * air_speed_build) * air_accel


Loading…
Cancel
Save