Godot Version
4.5.1
Question
I’m new to Godot and struggling with a collision issue. I have a Player and an Enemy (both CharacterBody2D). When they run into each other the enemy starts “jumping” around and both characters’ speeds change unpredictably.
Enemy:
extends CharacterBody2D
@export var speed : int = 600
@onready var target : Vector2 = Vector2.ZERO
@onready var player_pos : CharacterBody2D = get_node("/root/Main/Player")
func _physics_process(delta: float) -> void:
_movement(delta)
func _movement(delta: float) -> void:
target = player_pos.global_position
velocity = position.direction_to(target) * speed
look_at(target)
move_and_slide()
Player:
extends CharacterBody2D
@export var speed := 700
@onready var click_pos: Vector2 = position
func _physics_process(delta: float) -> void:
_player_movement(delta)
func _player_movement(delta: float) -> void:
if Input.is_action_pressed("mouse_move"):
click_pos = get_global_mouse_position()
velocity = position.direction_to(click_pos).normalized() * speed
look_at(click_pos)
if position.distance_to(click_pos) >10:
move_and_slide()