0 votes

aim_to immediately sets needed rotation for object. It's good for aiming player by mouse but aiming bots looks unnatural.

Is it possible to smoothly rotate object from previous position to new when you aim to new point?

in Engine by (246 points)

2 Answers

0 votes

you can use

lerp($cameranode.rotation.x, aim_to($player.x), 0.5 #adjust this)
lerp($cameranode.rotation.y, aim_to($player.y), 0.5 #adjust this)
lerp($cameranode.rotation.z, aim_to($player.z), 0.5 #adjust this)

function.

by (439 points)
0 votes

I assume this should work but I haven't tested it yet.

export var angular_acceleration = 2

func lerp_angle(from, to, weight):
    return from + short_angle_dist(from, to) * weight

func short_angle_dist(from, to):
    var max_angle = PI * 2
    var difference = fmod(to - from, max_angle)
    return fmod(2 * difference, max_angle) - difference

func _physics_process(delta):
    var direction : Vector2 = target_position - global_position
    direction = direction.normalized()
    rotation = lerp_angle(rotation, direction.angle(), angular_acceleration * delta)
by (246 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 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.