Random movement from right to left of an enemy

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lokigamer00

how do I make an enemy move randomly? type space invader that ships move to the right and left randomly mind

i need help ):

lokigamer00 | 2020-01-04 00:30

:bust_in_silhouette: Reply From: johnygames

You need to translate the enemy using a Vector of random numbers. In Godot you generate pseudo-random numbers in a few ways:

First you generate 2 random numbers (or 3 if you have a 3D game and want the enemy to move in all three directions). Then you set the velocity of the enemy to be equal to the randomly generated numbers like this:

extends Sprite

var rng = 0
func _process(delta):
	randomize()
	rng = rand_range(-15.0, 15.0)
	translate(Vector2(rng,rng)*delta)

This moves the object by a random vector every tick of the game. You can make it more sophisticated.

Does this answer you question? Please Mark the answer as best if it does.

you cannot make a movement to the right and when you touch with the limit fence to the left, and from the left when you touch the limit to the right, and so

lokigamer00 | 2020-01-04 18:54

i need help speed ):

lokigamer00 | 2020-01-04 20:02