Canon only "recoiling" along z-axis after swapping model?

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.
example

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

If everything worked fine before changing models and the code has not been altered, then it is almost certainly either your animation channel (which seems unlikely since it’s based on the position as seen in the inspector) or it’s something to do with your scene tree organization.

In my own project, I have an unmodified Node3D called LogicalCannonConnector that acts as a swivel for the cannon. It’s basically this:
th-1984524166

I make this node look at the targets instead of the cannon itself, allowing the cannon to inherit LCC’s rotation so that it doesn’t disrupt the animation. I think this might be explained somewhere near the end of this section, but I could be wrong.

Turret object’s scene tree (TurretBase and Cannon are the course-provided glTF assets):
cap

2 Likes

adding in the second buffer node between the cannon and turretbase seems to have resolved the issue. My original home-made turret had something like 8 pieces (I got carried away with details) which made it hard to translat into using only 2, I guess that may have been the same thing I did originally. I had the shots coming from a “barrel” mesh but the canon itself was aiming itself AND the barrel.

Sounds like that was the issue. Thanks!

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms