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

@ -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