Better bullet traces and raycast for hitscan

This commit is contained in:
2025-01-26 10:59:59 +01:00
parent 1c8458048c
commit 7da4a3d6bb
26 changed files with 1444 additions and 292 deletions

View File

@ -0,0 +1,16 @@
extends Node3D
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func _on_head_collision_body_part_hit(dam: Variant) -> void:
print("head is hit")
pass # Replace with function body.

View File

@ -1,4 +1,7 @@
[gd_scene load_steps=9 format=4 uid="uid://1txob6jskn5s"]
[gd_scene load_steps=12 format=4 uid="uid://1txob6jskn5s"]
[ext_resource type="Script" path="res://scenes/characters/blue/dummy.gd" id="1_cyeub"]
[ext_resource type="Script" path="res://scenes/characters/blue/head_collision.gd" id="1_lianf"]
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_gqjtb"]
resource_name = "Alpha_Joints_MAT"
@ -255,6 +258,10 @@ blend_shape_mode = 0
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_j3emx"]
albedo_color = Color(0, 0, 1, 1)
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_k5bjr"]
radius = 11.245
height = 27.8
[sub_resource type="Animation" id="Animation_6g1lc"]
resource_name = "Run Forward"
length = 0.533333
@ -861,6 +868,7 @@ _data = {
[node name="Dummy" type="Node3D"]
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0, 0)
script = ExtResource("1_cyeub")
[node name="Body" type="Node3D" parent="."]
@ -1333,7 +1341,25 @@ mesh = SubResource("ArrayMesh_8h4ki")
skin = SubResource("Skin_hwx5g")
surface_material_override/0 = SubResource("StandardMaterial3D_j3emx")
[node name="HitCollisions" type="Node3D" parent="Body/Armature/Skeleton3D"]
[node name="BoneAttachment3D" type="BoneAttachment3D" parent="Body/Armature/Skeleton3D"]
transform = Transform3D(0.978017, -0.176546, -0.110972, 0.15839, 0.282801, 0.94601, -0.135631, -0.942791, 0.304547, -11.8203, 17.7472, -135.936)
bone_name = "mixamorig_Head"
bone_idx = 5
[node name="HeadCollision" type="Area3D" parent="Body/Armature/Skeleton3D/BoneAttachment3D" groups=["body"]]
transform = Transform3D(9.90229, 1.3945, -6.09554e-08, 0, -4.37114e-07, -10, -1.3945, 9.90229, -4.32843e-07, 0.58762, 7.43451, 1.4646)
script = ExtResource("1_lianf")
[node name="Collision" type="CollisionShape3D" parent="Body/Armature/Skeleton3D/BoneAttachment3D/HeadCollision"]
shape = SubResource("CapsuleShape3D_k5bjr")
[node name="CSGCylinder3D" type="CSGCylinder3D" parent="Body/Armature/Skeleton3D/BoneAttachment3D"]
[node name="AnimationPlayer" type="AnimationPlayer" parent="Body"]
libraries = {
"": SubResource("AnimationLibrary_dbxeu")
}
[connection signal="body_part_hit" from="Body/Armature/Skeleton3D/BoneAttachment3D/HeadCollision" to="." method="_on_head_collision_body_part_hit"]

View File

@ -0,0 +1,16 @@
extends Area3D
@export var damage: int = 100
signal body_part_hit(dam)
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func hit():
emit_signal("body_part_hit", damage)

View File

@ -1,6 +1,7 @@
[gd_scene load_steps=8 format=3 uid="uid://b10lpwfjgxds4"]
[gd_scene load_steps=9 format=3 uid="uid://b10lpwfjgxds4"]
[ext_resource type="Script" path="res://scripts/player/player_input_controller.gd" id="1_q75ai"]
[ext_resource type="Texture2D" uid="uid://oopj5mj1vdp0" path="res://assets/crosshairs/crosshair_default.png" id="2_lsd7c"]
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_jc50w"]
radius = 0.3
@ -90,7 +91,11 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.609154, 0)
fov = 40.0
[node name="BulletStartingPoint" type="Node3D" parent="FirstPersonCameraMount"]
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0, -0.343115)
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0, -0.42185)
[node name="AimRay" type="RayCast3D" parent="FirstPersonCameraMount/BulletStartingPoint"]
target_position = Vector3(0, 0, 1000)
hit_from_inside = true
[node name="GunMount" type="Node3D" parent="FirstPersonCameraMount"]
@ -99,5 +104,31 @@ libraries = {
"": SubResource("AnimationLibrary_u1b6p")
}
[node name="HUD" type="Control" parent="FirstPersonCameraMount"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = 8.0
offset_right = 8.0
grow_horizontal = 2
grow_vertical = 2
mouse_filter = 2
[node name="TextureRect" type="TextureRect" parent="FirstPersonCameraMount/HUD"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -36.0
offset_top = -36.0
offset_right = 36.0
offset_bottom = 36.0
grow_horizontal = 2
grow_vertical = 2
texture = ExtResource("2_lsd7c")
[node name="ModelMount" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.790262, 0)

15
scenes/maps/csg_box_3d.gd Normal file
View File

@ -0,0 +1,15 @@
extends CSGBox3D
var health = 5
func take_damage():
health -= 1
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if health < 1:
queue_free()

File diff suppressed because it is too large Load Diff

View File

@ -2,8 +2,9 @@ extends Node3D
const SPEED = 715
@onready var mesh = $MeshInstance3D
@onready var ray = $RayCast3D
@onready var mesh = $RigidBody3D/MeshInstance3D
@onready var rigid_body_3d: RigidBody3D = $RigidBody3D
@onready var ray = $RigidBody3D/RayCast3D
@onready var particles = $GPUParticles3D
# Called when the node enters the scene tree for the first time.
@ -14,11 +15,15 @@ func _ready() -> void:
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
position += transform.basis * Vector3(0, 0, SPEED) * delta
ray.collision_mask = 1
ray.enabled = 1
rigid_body_3d.set_use_continuous_collision_detection(true)
#ray.collision_mask = 1
#ray.enabled = 1
ray.target_position.z = SPEED / Engine.get_frames_per_second()
if ray.is_colliding():
mesh.visible = false
rigid_body_3d.visible = false
particles.emitting = true
#if ray.get_collider().is_in_group("body"):
#print("head")
await get_tree().create_timer(1.0).timeout
queue_free()

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=7 format=3 uid="uid://b0agqeg53tey5"]
[gd_scene load_steps=8 format=3 uid="uid://b0agqeg53tey5"]
[ext_resource type="Script" path="res://scenes/weapon/bullet.gd" id="1_th28m"]
@ -8,6 +8,10 @@ size = Vector3(0.2, 0.2, 0.5)
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_p8o05"]
albedo_color = Color(0.894987, 0.000702324, 0.905119, 1)
[sub_resource type="CylinderShape3D" id="CylinderShape3D_l3sdq"]
height = 0.3
radius = 0.08
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_ynv38"]
albedo_color = Color(0.929412, 0, 0, 1)
emission_enabled = true
@ -27,33 +31,42 @@ linear_accel_max = 6.67
size = Vector3(0.05, 0.05, 0.05)
[node name="Bullet" type="Node3D"]
visible = false
script = ExtResource("1_th28m")
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
[node name="RigidBody3D" type="RigidBody3D" parent="."]
mass = 0.01
continuous_cd = true
[node name="MeshInstance3D" type="MeshInstance3D" parent="RigidBody3D"]
transform = Transform3D(0.663102, 0, 0, 0, 0.601826, 0, 0, 0, 0.58984, 0, 0, 0)
mesh = SubResource("BoxMesh_vxst2")
skeleton = NodePath("../..")
surface_material_override/0 = SubResource("StandardMaterial3D_p8o05")
[node name="RayCast3D" type="RayCast3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.100459)
target_position = Vector3(0, 0, -0.5)
[node name="RayCast3D" type="RayCast3D" parent="RigidBody3D" groups=["body"]]
exclude_parent = false
target_position = Vector3(0, 0, 2)
hit_back_faces = false
collide_with_areas = true
[node name="Timer" type="Timer" parent="RigidBody3D"]
wait_time = 10.0
one_shot = true
autostart = true
[node name="CollisionShape3D" type="CollisionShape3D" parent="RigidBody3D"]
transform = Transform3D(1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 0)
shape = SubResource("CylinderShape3D_l3sdq")
[node name="GPUParticles3D" type="GPUParticles3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0.18476)
material_override = SubResource("StandardMaterial3D_ynv38")
emitting = false
amount = 10
lifetime = 6.29
one_shot = true
explosiveness = 1.0
process_material = SubResource("ParticleProcessMaterial_58cgi")
draw_pass_1 = SubResource("BoxMesh_mfkmi")
[node name="Timer" type="Timer" parent="."]
wait_time = 10.0
one_shot = true
autostart = true
[connection signal="timeout" from="Timer" to="." method="_on_timer_timeout"]
[connection signal="timeout" from="RigidBody3D/Timer" to="." method="_on_timer_timeout"]

View File

@ -3,7 +3,6 @@ extends Node3D
const SPEED = 715
@onready var mesh = $Object_10
@onready var trace = $Trace
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
@ -13,12 +12,6 @@ func _ready() -> void:
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
position += transform.basis * Vector3(0, 0, SPEED) * delta
trace.collision_mask = 1
trace.enabled = 1
if trace.is_colliding():
mesh.visible = false
await get_tree().create_timer(1.0).timeout
queue_free()
func _on_timer_timeout():

View File

@ -52,9 +52,5 @@ transform = Transform3D(1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0,
mesh = SubResource("ArrayMesh_uqgp1")
skeleton = NodePath("")
[node name="Trace" type="RayCast3D" parent="."]
target_position = Vector3(0, 0, 1)
hit_back_faces = false
collide_with_areas = true
[node name="Timer" type="Timer" parent="."]
wait_time = 0.2

View File

@ -4,23 +4,30 @@ extends Node3D
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
@onready var barrel: Node3D = $Barrel
@onready var bullet_trace_distance: Node3D = $BulletTraceDistance
@onready var bullet_trail_end: Node3D = $BulletTrailEnd
func shoot() -> Error:
var bullet_start_node: Node3D = bullet_trace_distance.find_child("Start")
var bullet_end_node: Node3D = bullet_trace_distance.find_child("End")
if bullet_start_node and bullet_end_node:
var path := "res://scenes/weapon/misc/bullet_trail_generic.tscn"
if not ResourceLoader.exists(path):
return ERR_DOES_NOT_EXIST
var scene: PackedScene = ResourceLoader.load(path)
if not scene.can_instantiate():
return ERR_CANT_OPEN
var node: MeshInstance3D = scene.instantiate()
node.init(bullet_start_node.global_position, bullet_end_node.global_position)
var root := get_tree().get_root()
root.add_child(node)
return OK
else:
push_warning("Couldn't generate a bullet trace, no distance node found")
return ERR_BUG
# -- TODO: It should not be hardcoded
var path := "res://scenes/weapon/guns/ak/bullet.tscn"
if not ResourceLoader.exists(path):
return ERR_DOES_NOT_EXIST
var scene: PackedScene = ResourceLoader.load(path)
if not scene.can_instantiate():
return ERR_CANT_OPEN
var node: Node3D = scene.instantiate()
var root := get_tree().get_root()
node.position = barrel.global_position
node.transform.basis = barrel.global_transform.basis
root.add_child(node)
return OK
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass

View File

@ -1,7 +1,8 @@
[gd_scene load_steps=54 format=4 uid="uid://dtvo21mk1webd"]
[gd_scene load_steps=55 format=4 uid="uid://dtvo21mk1webd"]
[ext_resource type="Script" path="res://scenes/weapon/guns/ak/with_hands.gd" id="1_2iqts"]
[ext_resource type="PackedScene" uid="uid://dab7jttp7ywfh" path="res://scenes/weapon/guns/ak/gun.tscn" id="1_aaafm"]
[ext_resource type="PackedScene" uid="uid://bjyltbtx45cqs" path="res://scenes/weapon/misc/bullet_trace_distance.tscn" id="3_5ff4y"]
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_tg27p"]
@ -734,5 +735,5 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.286444, 0, -0.397435)
mesh = SubResource("ArrayMesh_kj4i3")
skeleton = NodePath("")
[node name="Barrel" type="Node3D" parent="."]
transform = Transform3D(-1, 0, 8.74228e-08, 0, 1, 0, -8.74228e-08, 0, -1, 0, 0.161938, -4.38604)
[node name="BulletTraceDistance" parent="." instance=ExtResource("3_5ff4y")]
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, -3.86167e-07, 0.200684, -4.41724)

View File

@ -0,0 +1,8 @@
[gd_scene format=3 uid="uid://bjyltbtx45cqs"]
[node name="BulletTraceDistance" type="Node3D"]
[node name="Start" type="Node3D" parent="."]
[node name="End" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 300)

View File

@ -0,0 +1,22 @@
extends MeshInstance3D
func init(pos1, pos2):
var draw_mesh := ImmediateMesh.new()
mesh = draw_mesh
draw_mesh.surface_begin(Mesh.PRIMITIVE_LINES, material_override)
draw_mesh.surface_add_vertex(pos1)
draw_mesh.surface_add_vertex(pos2)
draw_mesh.surface_end()
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func _on_timer_timeout() -> void:
queue_free()

View File

@ -0,0 +1,20 @@
[gd_scene load_steps=3 format=3 uid="uid://balgqtw1ijpw3"]
[ext_resource type="Script" path="res://scenes/weapon/misc/bullet_trail_generic.gd" id="1_k8eg1"]
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_ybqvs"]
albedo_color = Color(0.934388, 0.967243, 0.955863, 1)
emission = Color(1, 1, 1, 1)
emission_energy_multiplier = 2.7
[node name="BulletTrailGeneric" type="MeshInstance3D"]
material_override = SubResource("StandardMaterial3D_ybqvs")
visibility_range_end = 1891.27
script = ExtResource("1_k8eg1")
[node name="Timer" type="Timer" parent="."]
wait_time = 0.05
one_shot = true
autostart = true
[connection signal="timeout" from="Timer" to="." method="_on_timer_timeout"]

View File

@ -0,0 +1,3 @@
[gd_scene format=3 uid="uid://bfelcp5dmag5r"]
[node name="MuzzleFlash" type="MeshInstance3D"]