The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

I have a playercontroller 2d in top-down view game, where the players ship follows the cursor.
I have already done that my ship rotates to cursor with a small latency
code below shows how it was done

var rotation_speed = PI * 1.5 # PI = 180 degrees

func _look_at_mouse(delta):
    var v = get_global_mouse_position() - global_position
    var angle = v.angle()
    var r = global_rotation
    var angle_delta = rotation_speed * delta
    angle = lerp_angle(r, angle + 1.57, 1.0)
    angle = clamp(angle, r - angle_delta, r + angle_delta)
    global_rotation = angle

But now I need my ship not only rotate with latency, but also to move not exactly to cursor global position, but to it's position with the same interpolation / latency as the rotation of the ship. I have no idea how to do it, tried many things. Code below represents my move function:

func _move_to_mouse(delta):
    if position.distance_to(get_global_mouse_position()) > stop_distance:
        var direction = get_global_mouse_position() - position
        var normalized_direction = direction.normalized()
        var direction_distance = direction.length()
        move_and_slide(normalized_direction * max(move_speed, direction_distance))

P.S. I copied this question from same one, because there was no proper answer. Or I just don't understand it. Sorry, if that's my bad.

Godot version v3.3.4.stable.official.
in Engine by (63 points)

1 Answer

0 votes
Best answer

You can rotate vectors by sprites rotation amount. So take in vector in "neutral direction" (be it right or up depending on sprite) and rotate.

var speed = Vector2(max(move_speed, direction_distance), 0)
speed = speed.rotated(global_rotation)
move_and_slide(speed)
by (1,100 points)
selected by
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.