Canons used to recoil toward the “rear” of whatever direction they were facing and after changing the models (and adding new animations since I could not do it the way Bram did in the lecture) I cannot seem to get the canons to recoil correctly. regardless which way they are firing they always recoil toward the “top” of the screen.
The animation is set to move the canon back 0.3 meters along the Z-axis, which was working before swapping models, my original gun would recoil away from the direction it was firing instead of being locked to the Z-axis.
The code has not been altered against what Bram did in the video but I am getting dissimilar results.
extends Node3D
@export var projectile: PackedScene
@export var turret_range := 10.0
var enemy_path: Path3D
var target: PathFollow3D
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var cannon: Node3D = $Cannon
@onready var turret_base: Node3D = $TurretBase
func _physics_process(delta: float) -> void:
target = find_best_target()
if target:
cannon.look_at(target.global_position, Vector3.UP, true)
func _on_timer_timeout() -> void:
if target:
var shot = projectile.instantiate()
add_child(shot)
shot.global_position = cannon.global_position
animation_player.play("shoot")
shot.direction = cannon.global_transform.basis.z
func find_best_target() -> PathFollow3D:
var best_target = null
var best_progress = 0
for enemy in enemy_path.get_children():
if enemy is PathFollow3D:
if enemy.progress > best_progress:
var distance = global_position.distance_to(enemy.global_position)
if distance < turret_range:
best_target = enemy
best_progress = enemy.progress
return best_target