Turret Shooting High

I have been following along, yet for the majority of the times, the projectiles go above the enemies and do not collide unless the turret is right next to them. I don’t know why this is happening

hi Tater,

is the bullet firing at a sort of upwards angle im guessing?

can we have a look at your turret.gd script please.

or if youre able to, can you zip the project folder up and pop it online so i can download it for a look to see if i can see anything that can get you back on track.

Darren

Yeah, the bullets would always fire high at an angle. I ended up changing some parts of the script to fix this where it does not use the basis.z anymore.

extends Node3D

@export var projectile: PackedScene
@export var turret_range :=  10.0
@onready var barrel = $TurretBase/TurretTop/Barrel
@onready var animation_player = $AnimationPlayer

var enemy_path: Path3D
var target: PathFollow3D


func _physics_process(delta):
	target = find_best_target()
	if target:
		look_at(target.global_position, Vector3.UP, true)


func _on_timer_timeout():
	if target:
		var shot = projectile.instantiate()
		add_child(shot)
		shot.global_position = barrel.global_position
		shot.direction = barrel.global_position.direction_to(target.global_position)
		animation_player.play("turret_fire")

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

I also had to adjust the enemies to have a v offset of 1 or else it would always fire low (still hitting the target,) but if it was a miss then the bullet would go too low and disappear

i wouldnt mind a look at the project if your still free to zip it up, does sound like theres a stray transform or a rotation.
i tried to replicate, but didnt get it going in this way.

if you still have the project, it would be good if i could find out why and let you and other students for a possible gotcha or issue.

Darren

Yeah I am not too sure what was happening. I was getting a bug with the fix that I did where if there were too many towers and an enemy died as one was shooting it would all crash.

I went back to using the basis.z but I switched it to using
shot.direction = gun.global_transform.basis.z.normalized()

I was messing around with it all so much that unfortunately I don’t think the zip would do much good for the original issue.

Here it is though https://drive.google.com/file/d/1o-b4NGU4PZO_jCajATXTvNHmONRtnqYf/view?usp=drive_link

1 Like

access request sent :+1:

1 Like

You should have access now

Cheers. I’ll grab in morning :+1:

Hi,
I have a similar problem. The enemies don’t disappear (Barbarian Blaster - damaging Enemies lesson)
I check the script a couple of times… I don’t know what is wrong.
The explanations are very very good but I am not sure that I have the full understanding to figure it out why is not working.
I have attached the project in a zip file.
Any help would be appreciate it.
Kind Regards
Barbarian Blaster Project - Damaging Enemies issue

Hi Antonio,
for your project, if you look at your enemy scene, you accidentally put the collision shape node in the enemy_area group, instead of the EnemyArea3D node. as the code in the projectile is checking the if the area3d is in a certain group.

Taters project, i couldnt get to the bottom of it after going back to it for a good while, i suspect it was some of the rotations and transforms that were slightly off.

Privacy & Terms