Random directions RigidBody2D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Rayu
onready var velo = set_linear_velocity(Vector2(rand_range(250, -250),rand_range(250, -250)))

This is my code for the randomized direction i use for my Rigidbody2D, because everytime the Rigidbody2D goes in a different direction with this code but the only issue i have with that is that the speed is different . I want it to be constantly one speed, like 250, and not inbetween 250 and -250.

:bust_in_silhouette: Reply From: Thomas Karcher

The speed is determined by the length of the vector. So you can either normalize this length and multiply with 250:

onready var velo = set_linear_velocity(Vector2(rand_range(250, -250),rand_range(250, -250)).normalized() * 250)

Or you can simply use a randomly rotated Vector with length 250:

onready var velo = set_linear_velocity(Vector2(250,0).rotated(randf() * 2.0 * PI))