In this script I want to have my enemy character preform a different attack when it is within a certain range.
It's a spin attack and I have it set to be +2 distance away from a normal attack range.
I want the probability of the spin attack to be random, like a 10% probability of occurring.
Anyway that I set this up it is constantly checking if it is within range of the spin attack, resulting in a 100% probability rate.
So I've tried to set up a boolean variable in hopes of having it check only once if it is within range.
I realize the 'return' at the end of that is doing nothing.
Does anyone know of a better way to achieve this?
How can I make an enemy's action occur randomly if within a certain range?
Yes, you could use a random number check. Basically, you would create a random number between 0 and 1 and when it is within a certain range, you can then check if the enemy is within range of the spin attack.
For example, something like this:
var random_number = random_range(0, 1);
if (random_number >= 0.9 && current_enemy_distance <= spin_attack_range + 2) {
// Perform the spin attack
}
Don't forget to randomise the number before hand and look up random number generation is the only thing I'd add.
- Edited
Lethn
Here is the new code with the AI assistance.
var random_number = random_range(0, 1)
func _in_attack_circle():
return random_number >= 0.5 && global_position.distance_to(player.global_position) <= ATTACK_RANGE +2
In my process function I have this.
anim_tree.set("parameters/conditions/spin_a", _in_attack_circle())
When my character is in range and the probability is greater than 0.5 it will play the animation
but it will continue playing the animation.