+1 vote

When the player enters an area, i made him stop moving and i also made an object showing at the same time. The question is, how can i make the camera (after the player stops) glide slowly to my object position, and after it got into position -> make the object appear? And after the object appears, the camera glides faster to the player.
(*The camera is a child of my player)

in Engine by (90 points)

2 Answers

+1 vote
Best answer

Edit - 2D: Use lerp to interpolate the global position to the target's global position.

onready var target = get_node("path/to/node")
var speed : float

func _process(delta):
    set_global_position(lerp(get_global_position(), target.get_global_position(), delta*speed)

For 3D: You could replace your camera with an InterpolatedCamera, or use a lerp. Because the camera is a child of the player you will need to use global_transform.origin, and InterpolatedCamera (from my experience) seems to use transform.origin instead. By all means try the InterpolatedCamera, if that doesn't work here's a lerp script.

onready var target = get_node("path/to/node")
var speed : float

func _process(delta):
   global_transform.origin = lerp(global_transform.origin, target.global_transform.origin, delta*speed)
   var current_rotation = Quat(global_transform.basis)
   var next_rotation = current_rotation.slerp(Quat(target.global_transform.basis), delta*speed)
   global_transform.basis = Basis(next_rotation)
by (3,223 points)
selected by

the script is for the camera that is a child to the player or for a camera which i include in the scene?

It doesn't matter because it's using global_transform not transform which would be local space.

It says "can't get index 'basis' on base 'Transform2D' " at the second line of the process function.

Oooohhhh 2D? *facepalm
My answer is for 3D, I'll edit it for 2D as well. I should've known by the "camera2d" tag.

Thank you so much for your patience and help! I was having difficulties with the camera being a child of the player(the player is an instanced scene) and i couldn't manage to change the target.
But I assigned a camera to the scene and it worked! And it's easier to code if the camera is a child of the scene, not a child of the player.

+2 votes

Making your camera a child of your player is a quick and easy way to have a camera precisely follow him, but it makes focusing the camera on other things difficult.

I'd suggest taking the camera out of the player and adding a target variable on the camera script. This will require you to rethink how that camera moves around though.

something like this:

extends Camera2D

var target
var speed

func _process(delta):
    var target_dir = (target.position - self.position).normalized()
    position += speed * target_dir * delta

This would let you set the target and camera speed to fit your needs. However, this implementation introduces the risk of having your target move faster than the camera, so you probably want to make sure the camera "sticks" to the target, whether by adding a "is_centered" bool that makes camera's position always take the target's position (ignoring speed and delta), or by adding some sort of boundaries to ensure the target doesn't leave the camera's viewport. To help with that last point, you could check out this video (although in this case the camera is a child of the player): https://www.youtube.com/watch?v=lNNO-Gh5j78&vl=en

by (1,114 points)

Thank you! I actually used your advice and took the camera out of the player, it was easier that way. I took Magso's code and combined it with your advice.

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.