Topic was automatically imported from the old Question2Answer platform.
Asked By
Swagilini
I’m brand new to coding in general (had a class for java in highschool) so any and all suggestions are welcome.
But I’m trying to make a simple ping-pong game as a learning experiment, and I want the player to follow the mouse with a slight lag behind the exact mouse position. I’ve got that working. However adding move_and_slide makes the player fly off screen, and move_and_collide crashes the game.
extends KinematicBody2D
func _physics_process(_delta: float) → void:
if position != get_global_mouse_position():
position = lerp (position, get_global_mouse_position(), .1)
func _physics_process(_dt: float) -> void:
var new_position = position
if position != get_global_mouse_position():
new_position = lerp(position, get_global_mouse_position(), .1)
move_and_collide(new_position - position) # relative position vector
or
func _physics_process(dt: float) -> void:
var new_position = position
if position != get_global_mouse_position():
new_position = lerp(position, get_global_mouse_position(), .1)
move_and_slide((new_position - position) / dt) # linear velocity
move_and_collide and move_and_slide update the position for you, so if you use them, you no longer need to upda†e position explicitly.