Open GregRGM opened 4 months ago
Hey, Thanks for using the project and I'm happy to help where I can. I haven't really put much thought into enemies for the FPS template and left it up to the user. That being said. I don't think the script for firing projectiles is currently suitable for use outside of the player. The main reason being that it assumes that the entity firing the projectile is using the camera to determine the target.
Enemies don't need the camera. And I can see you've allowed for that. However, when I've designed enemies that shoot rigid body projectiles I've never used a ray cast to determine a direction. Since the enemies most likely already knows the position of the player it can just fire towards it. The player needs a ray cast because we don't know where the player is aiming and if simply fired forward, because of perspective, the rigid body won't land where you expect it to.
A ray cast would be useful to determine if the enemy should fire a projectile but once it knows it can. I can't think of a reason to use one. I could be wrong here, this is just how I like to do it.
here is a very simple script that I am using for a game jam at the moment. All it does is take in a target and fire the projectile at the target. You can add variation and spread easily by manipulating the target position variable.
@export var spawn_point: Marker3D
@export var attack_rate: int = 3
@export var projectile_velocity: int = 2
@export var projectile_damage: float = 10
var target: CharacterBody3D
var cooldown: bool = false
func start_attack(new_target:CharacterBody3D)->void:
if not cooldown:
target = new_target
var new_rigid_body_projectile = rigid_body_projectile.instantiate()
var target_position = target.global_position
var direction:Vector3 = (target_position - spawn_point.global_position).normalized()
new_rigid_body_projectile.global_transform = spawn_point.global_transform
new_rigid_body_projectile.set_as_top_level(true)
new_rigid_body_projectile.set_linear_velocity(direction*projectile_velocity)
new_rigid_body_projectile.damage = projectile_damage
get_tree().get_root().add_child(new_rigid_body_projectile)
cooldown = true
get_tree().create_timer(attack_rate).timeout.connect(on_attack_cooldown)
func on_attack_cooldown()->void:
cooldown = false
For future releases I will look at abstracting the projectile class so that it can be used with more objects that need to fire a projectile.
I'm a little new to GDScript and I like the Template so far. I like it so much that I want to adapt the shooting logic to enemies.
Right now, I'm trying to adapt the RigidBody projectile logic to an enemy bullet but I discovered there's no logic that creates a raycast directly from the shooter itself. This makes it difficult for me to fire bullets that move in the correct direction.
Currently what happens is either the bullet fires downward and to the left of the enemy that shoots it or some random error pops up. If I could get help with this, I'd really appreciate it.
Here's code for my enemy_attack.gd:
Here's the code I've attempted to modify in Projectile.gd:
`
Any help would be greatly appreciated as I enjoy this template quite a bit.