#Make an object move around the game scene at random
4 messages · Page 1 of 1 (latest)
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:
Godot Engine documentation
Inherits: PhysicsBody2D< CollisionObject2D< Node2D< CanvasItem< Node< Object Inherited By: PhysicalBone2D Physics Body which is moved by 2D physics simulation. Useful for objects that have gravity ...
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"