How to make a moving obstacle with a collision box in Godot?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By RockZombie4

Recently, I have made a player in Godot which can move forward, backward, gets rotated with the mouse, and can dash. I have also added a collision box to it so it will (hopefully) be able to detect when it has collided with a moving obstacle. The problem is: I don’t know how to make a moving obstacle with a collision box. Here is the code for the player(sorry, I don’t have a link for the images):

extends KinematicBody2D
export (int) var speed = 200

var velocity = Vector2(250,250)

func get_input():
    velocity = Vector2()
    if Input.is_action_pressed("down"):
        velocity = Vector2(-speed, 0).rotated(rotation)
    if Input.is_action_pressed("up"):
        velocity = Vector2(speed, 0).rotated(rotation)
    if Input.is_action_pressed("dash"):
        velocity = Vector2(700, 0).rotated(rotation)
    if Input.is_action_pressed("dash2"):
        velocity = Vector2(-700, 0).rotated(rotation)
func _physics_process(delta):
    velocity = move_and_slide(velocity)

Can someone help me?

:bust_in_silhouette: Reply From: ponponyaya

Here’s some tips.
Using StaticBody2D node to create it. (Or using KinematicBody2D node to create it if you want the player stand on and move with it.)
And then use AnimationPlayer node to move it. (Using AnimationPlayer note is the most simple way in my opinion. If you want write more code you also can use Tween node or Timer node with code to move it.)

I don’t know what kind of moving obstacle you want to create. But use above tips you can create any kind of moving obstacle you want.

May I suggest using a KinematicBody2D with a moving obstacle because a StaticBody2D is not meant to move.

Ertain | 2022-02-22 05:52