–1 vote

I have a playercontroller 2d in top-down view game, where the players ship follows the cursor. When I increase cursor speed it rotates with it instantly. And I want players to feel some weight, so make the ship rotating with some delay. How can I do it? Would be grateful for help. Controller code below. (Player.gd)

extends KinematicBody2D

export var move_speed = 250
export var stop_distance = 20
var playercoord = Vector2()

func _process(delta):
    _look_at_mouse()
    _move_to_mouse()
    if G.player_alive == 1:
        playercoord = get_node("/root/MainScene/Player").get_position()

func _look_at_mouse():
    look_at(get_global_mouse_position())
    rotation_degrees = rotation_degrees + 90

func _move_to_mouse():
    if position.distance_to(get_global_mouse_position()) > stop_distance:
        var direction = get_global_mouse_position() - position
        move_and_slide(direction)

ALSO, I've got enemies, and I want them to have weight slightly bigger than player, to have him feel he's a bit faster and subtle than the enemies. It's part of enemies controller code:

it's in the phys-process:

    if player:
        if G.player_alive == 1:
            var direction = (player.position - position).normalized()
            move_and_slide(direction * MOVE_SPEED)
pass

func _look_at_player():
    look_at(playercoord)
    rotation_degrees = rotation_degrees + 90
Godot version v3.3.4.stable.official.
in Engine by (36 points)
edited by

2 Answers

0 votes
Best answer

I found a solution by myself:

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
by (36 points)

Not quite Yourself mate, that is rude. You take someones time and You are expected to credit him.

I appreciate everyone who helps such guys as me, because every one from time to time needs help, but in search of an answer, I tried different things, and tooked code from different people line by line. In the end, I modified it to meet the task by myself. So I can call this "my code" for reasons above.

0 votes

In other words You want to interpolate angle in time. There are few functions for that, like movetoward() , lerp(), lerpangle(). In those functions You set starting angle and desired angle as first 2 arguments, and interpolation value as second. If You use delta as interpolation value it will get close to angle with constant speed. You can use non-constant interpolation values for easing in or out. Check those functions in script documentation.

by (2,394 points)

Thanks for assistance. I noticed, that these functions use float values, and I move and rotate objects by using coordinates and Vector values.

rotation_degrees = lerp(rotation, rotation_degrees + 90, -1.5)

tried to do this like that, but it seems I don't get something

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 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 webmaster@godotengine.org with your username.