Godot Version
Godot 4.6.2. stable
Question
I’m trying to make an enemy that shoot projectiles that can be hit, the enemy is perfectly fine except when I move to higher ground the enemy shoots the ground under me
I’m not really sure which script is at fault, but I assume it’s this one v
extends Node3D
class_name BowWeapon
@export var EXPLOSION_IMPULSE: float = 25.0
@export var show_aim := true
@export var aim_high := false
@export var ammo_resource:PackedScene
@onready var spawn_point: Node3D = $"."
@export var target: Node3D = null
var target_rotation_x: float = 0 ## controls hight rotaion
var target_rotation_y: float = 0 ## rotate horizontally
func _ready():
var tween = create_tween()
tween.tween_property(self, "position:y", 0.5, 1).set_trans(Tween.TRANS_CUBIC).as_relative().set_ease(Tween.EASE_OUT)
tween.tween_property(self, "position:y", -0.5, 1).set_trans(Tween.TRANS_CUBIC).as_relative()
tween.set_loops()
tween = create_tween()
tween.tween_property(self, "global_rotation:y",TAU,5).as_relative()
tween.set_loops()
func _physics_process(delta: float) -> void:
if show_aim:
draw_aim()
get_shot_direction()
if target and is_instance_valid(target):
aim_at_target()
func get_forward_direction():
return -basis.z
func get_shot_direction():
# difine Euler angles in deg
var euler_angles = Vector3(target_rotation_x, target_rotation_y, 0)
# create a basis from Euler angle
var base = basis.from_euler(euler_angles)
# the forward vector is typically the -z axis in the basis
var dir = -base.z
#DebugDraw.draw_line_relative_thick(spawn_point.global_position, dir*10, 3, Color.RED)
return dir
## give instinciated cannonball and the cannon will handle the forces
func do_attack() -> void:
var projectile = ammo_resource.instantiate()
get_tree().current_scene.add_child(projectile)
projectile.global_position = spawn_point.global_position
var dir: Vector3 = get_shot_direction()
if projectile is RigidBody3D:
projectile.linear_velocity = dir*EXPLOSION_IMPULSE
elif projectile is CharacterBody3D:
projectile.velocity = dir*EXPLOSION_IMPULSE
func draw_aim()-> void:
var vel: Vector3 = get_shot_direction()
vel *= EXPLOSION_IMPULSE # Impulse with mass 1, so equal velocity
var tstep := 0.1 #how much time in each iteration
var start_pos := spawn_point.global_position
var g: float = -ProjectSettings.get_setting("physics/3d/default_gravity", 9.8)
var line_start := start_pos
var line_end := start_pos
var colors = [Color.WHITE_SMOKE, Color.RED]
for i in range(1, 51): # from 1 to 5
var dt := (i * tstep)
line_end.x = dt * vel.x
line_end.z = dt * vel.z
line_end.y = dt * vel.y + (g*dt*dt)/2.0
line_end += start_pos
DebugDraw.draw_line(line_start, line_end, colors[i%2])
line_start = line_end
func aim_at_target():
if EXPLOSION_IMPULSE <= 0: return
var target_pos := target.global_position
#predict enemy velocity
#t = x?v0*cos(a)
if "velocity" in target:
var dist = global_position.distance_to(target_pos)
var t = dist/(EXPLOSION_IMPULSE * cos(target_rotation_x))
target_pos += target.velocity * t
var plane_target := target_pos
plane_target.y = global_position.y
## get plane direction to player (without y variation)
var dir := global_position.direction_to(plane_target)
# get the angle between cannon front (right) and the direction
target_rotation_y = dir.signed_angle_to(Vector3.FORWARD, Vector3.DOWN)
get_angle_to_target(plane_target, target_pos)
func get_angle_to_target(plane_target:Vector3, target_pos: Vector3):
var v = EXPLOSION_IMPULSE
var g: float = ProjectSettings.get_setting("physics/3d/default_gravity", 9.8)
var x := global_position.distance_to(plane_target)
var y := target_pos.y - global_position.y