Welcome to the forums @Barzenoki!
The normalized
function is defined in the vector2
class, not the float
class, which is why you are getting an error. However, I think the normalized
function isn't what you are needing if you are looking to have something move only on the X axis, as the normalized function just makes the length/magnitude of the Vector equal to 1
.
To have it only move on the X axis until you reach a certain point, what you probably want to do is just set the y
value of the vector to 0
. For example:
func _process(delta):
# get the direction from an enemy node ($Enemy) to the player ($Player)
var direction = $Enemy.global_position.direction_to($Player.global_position)
# remove the Y component so the enemy only moves on
# the X axis if they are left of position 300
if ($Enemy.global_position.x < 300):
direction.y = 0;
# move the enemy 200 pixels towards the player per second
$Enemy.global_position += direction * delta * 200
# Bonus: if we do not want to use direction_to, then we can get the
# relative vector and normalize it to get the same result
# (code) var manual_direction = ($Enemy.global_position - $Player.global_position)
# normalize it so it only gives us a direction.
# Otherwise the enemy will move faster when far away and slower when close.
# (code) manual_direction.normalize()
# Now manual_direction is the same as the result of "direction_to"
# and we can use it the same way we used "direction" in the code above
Then it would only move on the X axis until it got past the position 300
on the X axis.
If you share the code you are using from the tutorial, we can take a look at it and see if we can help provide a specific solution. You can copy-paste it into the forums and add three ~
to the beginning and end to render it as a code block.
Then we can look at it and try to figure out what is going on :smile: