Godot Version
4.1.1.stable
Question
Hi, I have my Player (KinematicBody2D), TileMap and a vehicle (KinematicBody2D or RigidBody2D?). If player collide with the vehicle, I want to push him in front of the vehicle. Now, the vehicle can push the player through the TileMap. I want to avoid stucking in the TileMap, so in this case I want to push him to some side and let the vehicle drive (yes, I want vehicle to go through the TileMap). How can I do that?
Thanks for advice
Here is code of vehicle:
extends CharacterBody2D
const speed = 100
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
var velocity = Vector2.DOWN * speed * delta
move_and_collide(velocity)
Here is code of player:
extends CharacterBody2D
@export var speed = 1600
@export var cur_hp : int = 10
@export var max_hp : int = 10
@export var min_hp : int = 0
# Called when the node enters the scene tree for the first time.
func _ready():
$AnimatedSprite2D.play("idle")
func _physics_process(delta):
get_input()
move_and_slide()
func get_input():
var input_x = 0
var input_y = 0
if Input.is_action_pressed("ui_left"):
input_x -= 1
elif Input.is_action_pressed("ui_right"):
input_x += 1
if Input.is_action_pressed("ui_down"):
input_y += 1
elif Input.is_action_pressed("ui_up"):
input_y -= 1
if input_x < 0:
velocity.x = -speed
$AnimatedSprite2D.play("left")
elif input_x > 0:
velocity.x = speed
$AnimatedSprite2D.play("right")
else:
velocity.x = 0
if input_y > 0:
velocity.y = speed
if input_x == 0:
$AnimatedSprite2D.play("down")
elif input_y < 0:
velocity.y = -speed
if input_x == 0:
$AnimatedSprite2D.play("up")
else:
velocity.y = 0
if velocity.x == 0 and velocity.y == 0:
$AnimatedSprite2D.play("idle")