Godot Version
4.3
Question
I’m trying to make an opponent paddle (CharacterBody2D) follow the “y” of a moving object, specifically, a ball (CharacterBody2D). That ball is for now bouncing around the screen. I want a paddle to follow it. I was able to do that by doing self.global_position.y = ball.global_position.y. The problem is, it exactly follows it. I want some kind of a delay, some kind of speed implemented on the paddle so that it won’t exactly follow the ball.
This is the code for the opponent paddle:
extends CharacterBody2D
class_name Opponent
@onready var ball_node: CharacterBody2D = get_node("/root/Game/Ball")
var _speed: float = 100.0
func _ready() -> void:
pass
func _physics_process(delta: float) -> void:
self.global_position.y = ball_node.global_position.y
self.global_position.y = lerp(ball_node.global_position.y, self.global_position.y, _speed)
move_and_slide()