Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | rubendw03 |
Hi, I have an enemy in a topview game. At first I had the enemy as a rigidbody but then it slowed down on collisions so I turned it into a kinematic body but now I have no idea how to make the enemy move in a random direction and so it bounces off other rigidbodies.
This is how far I’ve come but this isn’t working out at all…
export (int) var speed
var rng = RandomNumberGenerator.new()
var velocity = Vector2(speed, 0)
var direction = rotation + PI / 2
func _ready():
rng.randomize()
direction += rng.randf_range(-PI / 4, PI / 4)
get_node(".").rotation = direction
func _physics_process(delta):
move_and_collide((velocity * delta).rotated(direction))
# Bounce
var collision = move_and_collide(velocity * delta)
if collision:
velocity = velocity.bounce(collision.normal)
if collision.collider.has_method("hit"):
collision.collider.hit()