Tips for coding an attack

Godot Version

v4.7.1.rc1.official [17e2686e0]

Question

My first game is currently in a trial and error phase of development, where I code in something and test until it works in someway. I manage to program a start of an attack move in the my playable characters code.

Also my game is 3d action platformer game similar to Skylanders

Example: Here

Note: I’ll be updating the code now and then from the feedback I get here.

Here’s my Playable character’s code:

extends CharacterBody3D

const HEALTH = 120
const SPEED = 5.0
const JUMP_VELOCITY = 6.0
const prime_attack_damage = 20

func _physics_process(delta: float) -> void:
	
	if not is_on_floor():
		velocity += get_gravity() * delta
#Basic movemnet
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY


	var input_dir := Input.get_vector("left", "right", "forward", "backward")
	var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		velocity.x = direction.x * SPEED
		velocity.z = direction.z * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
		velocity.z = move_toward(velocity.z, 0, SPEED)
	move_and_slide()

#Attacks
func prime_attack():

	var input_prime_attack := Input.is_action_just_pressed("primary attack")
	#play animation

What’s your rationale for assigning the input event to a variable? I’d look at that firstly.

Next, think about what you want to happen when the attack button is pressed. Is there an enemy near the player? How do you detect that? Do you subtract your attack damage from their health? Is there a probability of missing or a critical hit? Think of questions like these.

Your movement logic is completely fine but the next challenge for you is detecting whether or not there is an enemy nearby that can be attacked. If you solve that problem, inflicting damage on them is fairly simple.

My suggestion would be to use Area3D to detect if there’s something around to hit and have a method on the enemy to check if it has been hit rather than your player script checking if something else was hit. Much easier approach.

Get familiar with signals and collisions. Read the following docs until your eyes fall out and you’ll be on the right track:

Should I put a script in the Area3d?

You don’t have to, signals can be connected to any script within the scene (or through code any node in general). It’s up to preference but I’d recommend connecting to your player script as it already has a attack damage variable

So? do I need make a script that quickly check for enemies or breakables, if any are there, they get hit?

The Area3D will detect when objects overlap it, each new overlap will trigger a signal either body_entered or area_entered, it’s very likely you will only have to connect body_entered. You can connect this signal to any script, if you want to make a new script you can. Once connected you need to check if the overlapping body is one of those breakable/enemy types, you can do this a number of ways such as if body.is_in_group("breakable"): or if you use class_names for your scripts if body is Enemy:, then you can call the appropriate function to handle getting hit.

This is pretty fundamental standard stuff, I’d highly recommend checking out a tutorial for basic attacks/hit detection. The dimension doesn’t matter, 2D/3D both handle collisions and respond accordingly. The likeness to other games or specificity of your idea doesn’t matter, they are built on top of this foundation of game development and so too will you build on top of this, but first you must recognize and use the foundations.

this is my 1st time

I understand, that’s why I and others will be recommending tutorials. The people helping on forum are helping, and generous, but usually looking for little puzzles to solve, we want to spend 10 minutes or less writing out what we think will get you to the next step. If you don’t have the fundamentals it’s usually explained better elsewhere and the open-ended-ness of such problems means it’s hard to recommend a next step, not without a lot more time spent going over why and when you might make such decisions, such as if you want a script on the Area3D or your player.

Make mistakes, learn from them and ask for help! We love a good error message, not as much pontificating design decisions.