I'm working on a game in wich I want a dot to move in a field, when it hits the border walls it should bounce off. I've got all these things to work except for the RigidBody2D (the dot) to move at a constant pace.
This is the basic script:
extends RigidBody2D
export (int) var speed
signal infected
var rng = RandomNumberGenerator.new()
func _ready():
set_bounce(1)
rng.randomize()
var direction = rng.randf_range(-PI, PI)
get_node(".").rotation = direction
get_node(".").set_linear_velocity(velocity.rotated(direction))
I tried this to give the RigidBody2D a constant pace but it didn't work out:
extends RigidBody2D
export (int) var speed
signal infected
var rng = RandomNumberGenerator.new()
var velocity = Vector2(speed, 0)
func _ready():
set_bounce(1)
rng.randomize()
var direction = rng.randf_range(-PI, PI)
get_node(".").rotation = direction
get_node(".").set_linear_velocity(velocity.rotated(direction))
func _physics_process(delta):
if get_node(".").linear_velocity <= Vector2(speed, 0):
velocity += Vector2(1, 0)
get_node(".").set_linear_velocity(velocity)
Does someone know a way to do this?