0 votes

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
angleto(Vector2(300, rand))
velocity.x = cos(angle)
velocity.y = sin(angle)
global
position += velocity * speed * delta

in Engine by (12 points)

1 Answer

+1 vote

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.

by (72 points)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.