#Make an object move around the game scene at random

4 messages · Page 1 of 1 (latest)

vague elm
#

For "always moves in the same direction", is that because your random numbers are always giving you the same result? Like if you print rotation in _ready() it's always the same?

#

As for this:

and when it is supposed to turn the game screen flashes, as if it were trying to teleport somewhere

I think this is expected. To move a rigidbody that is not static, you need to use the integrate_forces callback:

https://docs.godotengine.org/en/stable/classes/class_rigidbody2d.html#class-rigidbody2d-method-integrate-forces

split mantle
#

Yeah, for this Brownian-like movement you should use a CharacterBody.

glad sonnet
# split mantle Yeah, for this Brownian-like movement you should use a CharacterBody.

so I removed some part of the code and changed it to a CharacterBody:

extends CharacterBody2D

var speed
var max_speed = 200
var min_speed = 100

func _ready():
    randomize()
    speed = randf_range(min_speed, max_speed)
    rotation = deg_to_rad(randf_range(0, 360))

    global_position = Vector2(556, 211)

func _physics_process(delta):
    var move_direction = Vector2.RIGHT.rotated(rotation)
    velocity = move_direction * speed
    move_and_collide(velocity * delta)

But for some reason, it always starts in a different place when I run the project? It does not respect the "global_position"