randomize function not working

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

Im trying to make a game where a zombie goes to a random y coordinate but when i run they keep going ot the same spot evn if i use randomize().
Heres the code:

extends KinematicBody2D
func random():
randomize()
return randi()%301
var velocity = Vector2(0, random())
var speed = 200
func _process(delta):
var rand = random()
var angle = get_angle_to(Vector2(300, rand))
velocity.x = cos(angle)
velocity.y = sin(angle)
global_position += velocity * speed * delta

:bust_in_silhouette: Reply From: Drogrant

Hi there,

I have tested it and the problem here is not the randomize function but the algorithm you have written. You are computing the angle to a random point in Y every frame and on average the kinematicbody wiggles to the same area.

What i would do is get the random point once when you create the zombie (maybe again when it reaches that point to make it wander around or whatever) and then instead of using the angles just find the direction with the difference of the vectors normalized.

Thanks a lot, I’m pretty new to Godot and haven’t learned trigonometry yet. I’m found the move script online and added a randomize script to it.

ion | 2020-08-07 16:45