0 votes

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 moveandslide makes the player fly off screen, and moveandcollide crashes the game.

extends KinematicBody2D

func physicsprocess(delta: float) -> void:
if position != get
globalmouseposition():
position = lerp (position, getglobalmouse_position(), .1)

this is my player code so far

Godot version 3.3.4
in Engine by (14 points)

1 Answer

0 votes

Try the following:

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.

by (70 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.