hello, Iam using the stepify the get a target x and z on a level, but it doesnt seem to work. --- Debugging process started ---

! ! --- Debugging process started --- ! ! mySTFY 1.2 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.2 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.3 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.3 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.3 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.3 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.3 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.3 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.3 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.3 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.3 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.3 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.3 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.4 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.4 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.4 ! AItriangle 0 ! ! ! ! ! targetZ 1.4 ! mySTFY 1.4 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.4 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.4 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.4 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.4 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.4 ! AItriangle 0 ! targetZ 1.4 ! mySTFY 1.4 ! --- Debugging process stopped ---

the enemy following the player stops and doesnt change state ?

	var targetX = stepify(target.translation.x, 0.1);
	var targetY = stepify(target.translation.y, 0.1);
	var targetZ = stepify(target.translation.z, 0.1);


	print("AItriangle "+ String (AItriangle));
	
	match AItriangle:
		0:#----//--DOWN--//---------
			print("targetZ " + String (targetZ +_distZ)  );
			print("mySTFY " + String (stepify(translation.z, 0.1) ) );
			
			var dirDown = Vector3( targetX, 0, targetZ +_distZ );

			if ( targetZ +_distZ != stepify(translation.z, 0.1)   ):
				_changeState("st_walk");
				motion = dirDown - transform.origin;
				motion = motion.normalized() * walkSP;
			else:
				AItriangle = 1;

Is there a way to change state once the enemy reaches the target position.z + value.z ?

Why don't set a breakpoint on line 15 and check what's going on?

Using the equality (==) or inequality (!=) operators with floating point values is unreliable. It's better to use the greater-than (>, >=) and less-than (<, <= ) operators .

@pkowal1982 said: Why don't set a breakpoint on line 15 and check what's going on?

Ive never used break points i dont really know how they work ? The times i used them, they didnt showed anything. In this case the function never changes to else: AItriangle = 1; a break point here wont do anything

@DaveTheCoder said: Using the equality (==) or inequality (!=) operators with floating point values is unreliable. It's better to use the greater-than (>, >=) and less-than (<, <= ) operators .

ive used the < and > but it only seems to work with a value of 0.2, anything diferent and it doesnt check

	match AItriangle:
		0:#----//--DOWN--//---------
			var dirDown = Vector3( targetX, 0, targetZ +_distZ );
			if ( dirDown.distance_to( transform.origin ) > 0.2  ):
			####if ( targetZ +_distZ != translation.z   ):
				_changeState("st_walk");
				motion = dirDown - transform.origin;
				motion = motion.normalized() * walkSP;
			else:
				AItriangle = 1;
			if ( is_on_wall() ):
				AItriangle = 1;
		1:#----//--LEFT--//---------
			
			var dirLeft = Vector3( targetX - _distX, 0, targetZ );
			if ( dirLeft.distance_to( transform.origin ) > 0.2  ):
			####if ( targetX - _distX != stepify(translation.x, 0.1)   ):
				_changeState("st_walk");
				motion = dirLeft - transform.origin;
				motion = motion.normalized() * walkSP;
			else:
				AItriangle = 2;
			if ( is_on_wall() ):
				AItriangle = 2;
		2:#----//--UP--//---------

if i round() the target.tranlation.z + the round() of self tranlation.z... i can get exact values both player and enemy have Z of 1.4... i dont know why i doesnt work ? I thinks its something to do with the motion = dirDown - transform.origin;

But its up there in the log ( the 1st post ) the print shows both enemy and player at 1.4 ?

I have it working, It seems i cant use stepify when i frist get x,y,z of the target this way should work

	var targetX = target.translation.x;
	var targetY = target.translation.y;
	var targetZ = target.translation.z;
 
			if ( stepify(targetZ +_distZ, 0.1) != stepify(translation.z, 0.1)   ):
				_changeState("st_walk");
				motion = dirDown - transform.origin;
				motion = motion.normalized() * walkSP;

I've used stepify, and it works as documented.

if ( stepify(targetZ +_distZ, 0.1) != stepify(translation.z, 0.1) ):

As I said above, you can't depend on that kind of comparison when using floats.

You could do this instead, so that you're comparing ints:

if int(round(stepify(targetZ +_distZ, 0.1) * 10.0)) != int(round(stepify(translation.z, 0.1) * 10.0)):

Or if you want to check if two floats are approximately equal, you can do something like this:

if abs(a - b) < 0.001:

@DaveTheCoder said:

Or if you want to check if two floats are approximately equal, you can do something like this:

if abs(a - b) < 0.001:

thanks this worked really well. Using the previous ways the enemy would slightly slow down its walking speed before reaching the target.

this it keeps its current speed and changes to the next state

if ( abs( (targetZ +_distZ ) - translation.z ) > 0.1 ):

You can use the is_equal_approx() or is_zero_approx() built-in functions instead of abs(a - b) < 0.001.

@Calinou said: You can use the is_equal_approx() or is_zero_approx() built-in functions instead of abs(a - b) < 0.001.

thanks, but it doesnt quite seem to work the enemy struggles to find the target, only on rare ocasions he changes state ?

target = get_tree().get_root().get_node("level/3d_player1");

var targetX = target.translation.x;
var targetY = target.translation.y;
var targetZ = target.translation.z;

		var dirDown = Vector3( targetX, 0, targetZ +_distZ );

		if (  !  is_equal_approx(targetZ +_distZ, translation.z)  ):
			_changeState("st_walk");
			motion = dirDown - transform.origin;
			motion = motion.normalized() * walkSP; #walkSP = 1.4;
			
		else:
			print("here")
			AItriangle = 1;
		if ( is_on_wall() ):
			AItriangle = 1;

[edit] the min() seems to work

if ( min( targetX +_distZ, translation.z ) < 0.001 ):

is_equal_approx() is basically doing abs(a - b) < abs(a) * 0.00001 so it is looking for values extremely close together. (based on Godot 4 source, I haven't looked in 3.4)

The enemy would have to be moving very slowly (or be very lucky) to land within that distance of the target. Having a custom tolerance value is easier.

(An object moving at 1.4 units per second at 60fps will jump 0.02333 units per frame, so would usually leap over the target)

10 months later