Im trying to make the enemies chase the player character only on the x-axis until a certain point, but i am getting this error each time i put an .x on 'player.position', the tutorial im using didnt declare normalized, nor do i know how to declare a pre-built function. Trying to figure it out on my own is not working at all, so any help would be great
Invalid Call. Nonexistent function 'normalized' in base 'float'
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:
- Edited
func _physics_process(delta):
if player:
_velocity = (player.position.x - position.x).normalized()
_velocity = move_and_slide(_velocity)
_velocity = _velocity.y + gravity
func _on_DetectRadius_body_entered(body):
player = body
func _on_DetectRadius_body_exited(body):
player = null
it runs if i use velocity = position.direction_to(player.position) * speed
but i can't use .x
for it
The parameter of direction_to() is a Vector2. player.position is a Vector2. player.position.x is a float, not a Vector2.
Is that your question?
It doesn't work because normalize is a function that works on vectors (a combination of two floats) and you are passing a single float. The operation also doesn't make any sense, cause even is normalized worked for floats, it would return 1.0 always, which would be useless. That is not the correct function.
@DaveTheCoder said: The parameter of direction_to() is a Vector2. player.position is a Vector2. player.position.x is a float, not a Vector2.
Is that your question?
yes, thank you for explaining it to me!
Thank you guys for the help! Im not sure the tutorial is made for the type of game im making ( especially since i need the enemies to stop at some point before running into the player, ill try looking for different ways to solve my problem.