Godot Version
Godot 4.3
Question
I figured out how to rotate my player towards an NPC, but am struggling to find a way to interpolate it for a smooth rotation, the code is below:
@onready var Player = %Myra
func _process(delta):
if Globalfunctions.can_move == false:
var target_position = self.transform.origin
Player.look_at(target_position, Vector3.UP, true)
The NPC is the one that is holding the script, and I assume the solution is something to do with the lerp() function, any help is appriciated!
Thanks!
Maybe using lerp would work, or a tween.
You could also calculate the angle to the npc, figure out the radians or degrees, divide them by an amount based on time you want it to take, then set that in players physics process.
Player #find npc location
#calculates angle to npc
#divides angle by chosen number
#in physics if var for rotation is true rotate by angle divided by number
#once angle to npc == 0 stop
Sorry not by my computer to type up proper code.
But honestly what gertkeno suggested might be easier.
You could use Basis’ looking_at
function to define a start and end point, then use the spherical lerp slerp
between the two.
Or if you only intend to animate the positions Tween has an exmaple using tween_method
here
I think the first Basis.looking_at
will work better for you.
2 Likes
I think I understand what you’re saying about the Basis.looking_at()
approach, I have been trying to find a way to write this in code but am struggling, would it be possible for you to provide an example of how to use it? It doesn’t have to be specific to the code I provided, I just don’t quite understand how to go about making it. Thank you for the responses!
var direction: Vector3 = self.global_position.direction_to(target_position)
var target: Basis = Basis.looking_at(direction)
# if in _process
Player.basis = Player.basis.slerp(target, 0.01)
Though this will be frame-dependent, how important to gameplay functions is this rotation? Is it only for visuals?