0 votes

i'm creating simple tank demo game. In scene player tank is centre-bottom position. now when use click on any area of game screen tank will be rotate toward that points.

i checked look_at() function but its effect is instant i want slowly animation of rotation.

e.g player point toward top-right direction now user click at left-top direction than tank should rotate to left-top direction.

Tank Structure is:

---- CollisionShape2D
---- Sprite

Does anyone have any idea how i can do this? Any link , tutorial or code will be helpful.

Thank you.

in Engine by (122 points)
edited by

1 Answer

+2 votes
Best answer

If the tank is stationary using a lerp() towards a varaible taraget rotation would work, but angles do not wrap around between 0 to 360 so the aim would turn multiple turns until it would catch up with the rotation.

This seems to work as expected even at rotating more then 180 degrees.

var target_angle = 0
var turn_speed = deg2rad(3)

func _process(delta):

    var dir = $barrel.get_angle_to( get_global_mouse_position() )

    if abs(dir)<turn_speed: #to close for full turn_speed
        $barrel.rotation += dir #this is just a look_at
        if dir>0: $barrel.rotation += turn_speed #clockwise
        if dir
by (911 points)
selected by

Thanks man :) this is working great . i just update it for both clockwise and ant-clockwise.

    var dir = get_angle_to(get_global_mouse_position())
    if abs(dir) < turn_speed:
        rotation += dir
        if dir>0: rotation += turn_speed #clockwise
        if dir<0: rotation -= turn_speed #anit - clockwise

Registered just to upvote this answer: worked perfectly for me! Thanks!

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.