Skip to content
This repository was archived by the owner on Sep 22, 2020. It is now read-only.

Commit 27df2ce

Browse files
Changed how RigidBody_hit_test.gd works. Now it takes rotation of the bullet into account,
which gives the RigidBody nodes more realistic behavior.
1 parent b7213f9 commit 27df2ce

9 files changed

+23
-38
lines changed

Bullet_script.gd

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func collided(body):
4141
# If it does, then call it, passing our global origin as the bullet collision point.
4242
if hit_something == false:
4343
if body.has_method("bullet_hit"):
44-
body.bullet_hit(BULLET_DAMAGE, self.global_transform.origin)
44+
body.bullet_hit(BULLET_DAMAGE, global_transform)
4545

4646
# Set hit_something to true because we've hit an object and set free ourself.
4747
hit_something = true

Grenade.gd

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func _process(delta):
5656
var bodies = blast_area.get_overlapping_bodies()
5757
for body in bodies:
5858
if body.has_method("bullet_hit"):
59-
body.bullet_hit(GRENADE_DAMAGE, global_transform.origin)
59+
body.bullet_hit(GRENADE_DAMAGE, body.global_transform.looking_at(global_transform.origin, Vector3(0,1,0)) )
6060

6161
# This would be the perfect place to play a sound!
6262

Player.gd

+1-1
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ func add_grenade(additional_grenade):
775775
grenade_amounts[current_grenade] = clamp(grenade_amounts[current_grenade], 0, 4)
776776

777777

778-
func bullet_hit(damage, bullet_hit_pos):
778+
func bullet_hit(damage, bullet_global_transform):
779779
# Removes damage from our health
780780
health -= damage
781781

RigidBody_hit_test.gd

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
extends RigidBody
22

3+
const BASE_BULLET_BOOST = 9;
4+
35
func _ready():
46
pass
57

6-
func bullet_hit(damage, bullet_hit_pos):
7-
# We get the directional vector pointing from the bullet hit position to our origin.
8-
var direction_vect = global_transform.origin - bullet_hit_pos
9-
# Normalize the directional vector (so distance doesn't change the knockback)
10-
direction_vect = direction_vect.normalized()
8+
func bullet_hit(damage, bullet_global_trans):
9+
var direction_vect = bullet_global_trans.basis.z.normalized() * BASE_BULLET_BOOST;
1110

12-
# Then we apply a local impulse at the hit position with a force pointed at the directional vector.
13-
# This gives the appearance of the bullet push the object on collision.
14-
apply_impulse(bullet_hit_pos, direction_vect * damage)
11+
apply_impulse((bullet_global_trans.origin - global_transform.origin).normalized(), direction_vect * damage)

StickyGrenade.gd

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func _process(delta):
102102
var bodies = blast_area.get_overlapping_bodies()
103103
for body in bodies:
104104
if body.has_method("bullet_hit"):
105-
body.bullet_hit(GRENADE_DAMAGE, global_transform.origin)
105+
body.bullet_hit(GRENADE_DAMAGE, body.global_transform.looking_at(global_transform.origin, Vector3(0,1,0)) )
106106

107107
# This would be the perfect place to play a sound!
108108

Turret.gd

+2-2
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,11 @@ func fire_bullet():
176176
node_raycast.force_raycast_update()
177177

178178
# If the ray hit something, get its collider and see if it has the 'bullet_hit' method.
179-
# If it does, then call it and pass the ray's collision point as the bullet collision point.
179+
# If it does, then call it and pass the ray's global transform so we can tell which direction the bullet game from
180180
if node_raycast.is_colliding():
181181
var body = node_raycast.get_collider()
182182
if body.has_method("bullet_hit"):
183-
body.bullet_hit(TURRET_DAMAGE_RAYCAST, node_raycast.get_collision_point())
183+
body.bullet_hit(TURRET_DAMAGE_RAYCAST, node_raycast.global_transform)
184184

185185
# Remove the bullet from the turret
186186
ammo_in_turret -= 1

Turret.tscn

+10-22
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ surfaces/4 = {
143143

144144
[sub_resource type="CylinderMesh" id=4]
145145

146+
custom_aabb = AABB( 0, 0, 0, 0, 0, 0 )
147+
flip_faces = false
146148
top_radius = 0.4
147149
bottom_radius = 1.4
148150
height = 2.0
@@ -160,6 +162,7 @@ flags_use_point_size = false
160162
flags_world_triplanar = false
161163
flags_fixed_size = false
162164
flags_albedo_tex_force_srgb = false
165+
flags_do_not_receive_shadows = false
163166
vertex_color_use_as_albedo = false
164167
vertex_color_is_srgb = false
165168
params_diffuse_mode = 0
@@ -265,6 +268,7 @@ flags_use_point_size = false
265268
flags_world_triplanar = false
266269
flags_fixed_size = false
267270
flags_albedo_tex_force_srgb = false
271+
flags_do_not_receive_shadows = false
268272
vertex_color_use_as_albedo = false
269273
vertex_color_is_srgb = false
270274
params_diffuse_mode = 0
@@ -309,21 +313,21 @@ _sections_unfolded = [ "Albedo" ]
309313
[sub_resource type="SphereMesh" id=11]
310314

311315
material = SubResource( 10 )
316+
custom_aabb = AABB( 0, 0, 0, 0, 0, 0 )
317+
flip_faces = false
312318
radius = 0.2
313319
height = 0.5
314320
radial_segments = 6
315321
rings = 3
316322
is_hemisphere = false
317323

318324
[node name="Turret" type="Spatial" index="0"]
319-
320325
script = ExtResource( 1 )
321326
use_raycast = false
322327

323328
[node name="Base" type="Spatial" parent="." index="0"]
324329

325330
[node name="Turret_Base" type="MeshInstance" parent="Base" index="0"]
326-
327331
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.273253 )
328332
layers = 1
329333
material_override = null
@@ -343,7 +347,6 @@ material/3 = null
343347
_sections_unfolded = [ "Transform" ]
344348

345349
[node name="Static_Body" type="StaticBody" parent="Base" index="1"]
346-
347350
editor/display_folded = true
348351
input_ray_pickable = true
349352
input_capture_on_drag = false
@@ -357,24 +360,20 @@ script = ExtResource( 6 )
357360
path_to_turret_root = NodePath("../..")
358361

359362
[node name="Collision_Shape" type="CollisionShape" parent="Base/Static_Body" index="0"]
360-
361363
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.68306, 0.587342 )
362364
shape = SubResource( 2 )
363365
disabled = false
364366

365367
[node name="Collision_Shape_2" type="CollisionShape" parent="Base/Static_Body" index="1"]
366-
367-
transform = Transform( -4.37114e-08, 0, -1, 0, 1, 0, 1, 0, -4.37114e-08, 0, 1.68306, 0.587342 )
368+
transform = Transform( -4.37114e-008, 0, -1, 0, 1, 0, 1, 0, -4.37114e-008, 0, 1.68306, 0.587342 )
368369
shape = SubResource( 2 )
369370
disabled = false
370371

371372
[node name="Head" type="Spatial" parent="." index="1"]
372-
373373
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3.75254, 0.549067 )
374374
_sections_unfolded = [ "Transform" ]
375375

376376
[node name="Turret_Head" type="MeshInstance" parent="Head" index="0"]
377-
378377
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.00935364, 0.493907 )
379378
layers = 1
380379
material_override = null
@@ -395,8 +394,7 @@ material/4 = null
395394
_sections_unfolded = [ "Geometry", "Transform" ]
396395

397396
[node name="Flash" type="MeshInstance" parent="Head" index="1"]
398-
399-
transform = Transform( 0.6, 0, 0, 0, -2.62268e-08, -0.6, 0, 0.6, -2.62268e-08, 0, 3.69553, -6.75926 )
397+
transform = Transform( 0.6, 0, 0, 0, -2.62268e-008, -0.6, 0, 0.6, -2.62268e-008, 0, 3.69553, -6.75926 )
400398
layers = 1
401399
material_override = null
402400
cast_shadow = 1
@@ -412,8 +410,7 @@ material/0 = SubResource( 5 )
412410
_sections_unfolded = [ "material" ]
413411

414412
[node name="Flash_2" type="MeshInstance" parent="Head" index="2"]
415-
416-
transform = Transform( 0.6, 0, 0, 0, -2.62268e-08, -0.6, 0, 0.6, -2.62268e-08, 0, 2.65687, -6.38286 )
413+
transform = Transform( 0.6, 0, 0, 0, -2.62268e-008, -0.6, 0, 0.6, -2.62268e-008, 0, 2.65687, -6.38286 )
417414
layers = 1
418415
material_override = null
419416
cast_shadow = 1
@@ -429,19 +426,16 @@ material/0 = SubResource( 5 )
429426
_sections_unfolded = [ "Geometry", "Transform", "material" ]
430427

431428
[node name="Ray_Cast" type="RayCast" parent="Head" index="3"]
432-
433429
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 4.35444, -2.734 )
434430
enabled = false
435431
exclude_parent = true
436432
cast_to = Vector3( 0, 0, -40 )
437433
collision_mask = 1
438434

439435
[node name="Barrel_End" type="Spatial" parent="Head" index="4"]
440-
441-
transform = Transform( -1, 0, -1.50996e-07, 0, 1, 0, 1.50996e-07, 0, -1, 0, 4.22162, -11.5493 )
436+
transform = Transform( -1, 0, -1.50996e-007, 0, 1, 0, 1.50996e-007, 0, -1, 0, 4.22162, -11.5493 )
442437

443438
[node name="Static_Body" type="StaticBody" parent="Head" index="5"]
444-
445439
editor/display_folded = true
446440
input_ray_pickable = true
447441
input_capture_on_drag = false
@@ -455,19 +449,16 @@ script = ExtResource( 6 )
455449
path_to_turret_root = NodePath("../..")
456450

457451
[node name="Collision_Shape" type="CollisionShape" parent="Head/Static_Body" index="0"]
458-
459452
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3.43577, -3.32666 )
460453
shape = SubResource( 6 )
461454
disabled = false
462455

463456
[node name="Collision_Shape_2" type="CollisionShape" parent="Head/Static_Body" index="1"]
464-
465457
transform = Transform( 1, 0, 0, 0, 0.922934, 0.384959, 0, -0.384959, 0.922934, 0, 1.90657, 0.297366 )
466458
shape = SubResource( 7 )
467459
disabled = false
468460

469461
[node name="Vision_Area" type="Area" parent="." index="2"]
470-
471462
editor/display_folded = true
472463
input_ray_pickable = false
473464
input_capture_on_drag = false
@@ -491,13 +482,11 @@ reverb_bus_amount = 0.0
491482
reverb_bus_uniformity = 0.0
492483

493484
[node name="Collision_Shape" type="CollisionShape" parent="Vision_Area" index="0"]
494-
495485
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 7, 0 )
496486
shape = SubResource( 8 )
497487
disabled = false
498488

499489
[node name="Smoke" type="Particles" parent="." index="3"]
500-
501490
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.85465, 0.50817 )
502491
layers = 1
503492
material_override = null
@@ -526,4 +515,3 @@ draw_passes = 1
526515
draw_pass_1 = SubResource( 11 )
527516
_sections_unfolded = [ "Draw Passes", "Process Material", "Time" ]
528517

529-

Weapon_Knife.gd

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func fire_weapon():
5050
continue
5151

5252
if body.has_method("bullet_hit"):
53-
body.bullet_hit(DAMAGE, area.global_transform.origin)
53+
body.bullet_hit(DAMAGE, area.global_transform)
5454

5555

5656
func reload_weapon():

Weapon_Rifle.gd

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func fire_weapon():
5151
if body == player_node:
5252
pass
5353
elif body.has_method("bullet_hit"):
54-
body.bullet_hit(DAMAGE, ray.get_collision_point())
54+
body.bullet_hit(DAMAGE, ray.global_transform)
5555

5656
# Remove the bullet from the mag
5757
ammo_in_weapon -= 1

0 commit comments

Comments
 (0)