How to use the camera? (SOLVED)

Hi. i want to create a camera that follows my character but i don’t know how the camera works =S i have read the manual but still don’t get it. Does anyone can give me a simple explanation step by step about how to add a camera that follows a character? Thank you :smile:

1 Like

What problem do you run into? Are you trying to make a 2D orthographic or 3D camera?

1 Like

2d ortographic. It’s a 2D platformer game and the camera have to follow the character.

Ok, and what problems do you run into? Do the game render according to the camera position?

1 Like

The problemmos that i don’t know how to create a camera that follows the character. If someone can explain step by step how to do that. I put a camera object to the character but then the camera shows another position and the background is black and can’t see the tile map.

The easiest way is probably this:

1. Create a game object containing the camera component. Make sure that the camera component acquires camera focus.
2. Place the game object at origo. The position of this game object now controls rendering and position 0,0 matches the lower left corner of the camera view.
3. Child the camera game object under the player character (just drag and drop in the collection). The camera game object is now transformed as a child to the character in the scene-graph and will follow its moves.
4. Offset the camera as you like. Remember that the camera game object position is lower left corner of view.

UPDATE 2023-03-27

  1. Add a Camera component to a game object. Make sure that the camera component acquires camera focus.
function init(self)
    msg.post("#camera", "acquire_camera_focus")
end
  1. Tell the render script to use the view and the projection of the camera:
function init(self)
    msg.post("@render:", "use_camera_projection")
end
  1. Check the Orthographic checkbox of the camera component to use an orthographic project (used for 2D games). With this option checked the camera will center on the game object it is attached to.
11 Likes

thanks! it’s working now.

1 Like

We need more small “straight-to-the-point” guides like this in the Docs. Thanks sicher!

6 Likes

Yes, that is a good idea. The very few code samples in the documentation is intended to be just that bit we obviously need to create many more.

2 Likes

Is there a way to create a lag in the camera before following the character ?

Sure, there are a number of ways, depending on how you want your camera design to work.

First it’s probably easiest to not child the camera under the player character (otherwise you will have to constantly compensate for character movement). Instead, add a script to the camera game object to listen to a message “set_camera_position”, “look_at”, “set_target” or similar (the name is arbitrary). The trick is to not set the camera position directly but somehow filter or interpolate it. An easy way is to use vmath.lerp() to just linearly interpolate between the current and the target position.

The “camera.script” on the camera manual page contains an example implementation. http://www.defold.com/doc/camera

The character then just have to send a message each frame (in update()) to the camera with the character’s new position.

3 Likes

Thanks for the help ! Ended up using the lerp function as suggested by you. :slight_smile:

Hi guys, I’ve read all that post but still don’t understand how to use camera :frowning:

I have a 2D platform game and I want my camera follow the character but I want my character in the middle of the camera :slight_smile:

How could I do please ?

Hi there! What are you having problems with? Did you manage to do any of the steps above?

1 Like

Yes, I did. My problem is that my character is not in the middle of my camera.

But it’s tracking the character?

If the camera is a game object childed to the character you can move it. 0,0 is by default the lower left corner of the screen. So just move the camera game object in the editor.

Well this is the part I don’t understand. Do I have to create a “character.collection” and import my “character.go” + “camera.go” ?

You need to place the character and camera game objects in a collection so you can parent-child them -affecting their place in the scene graph.

But you don’t need to create a new collection for that, if you don’t want to. If character.go is placed on main.collection (for instance) just place camera.go alongside and then drag it onto character.go in the collection outline to make it a child.

2 Likes

I got it :slight_smile: Thanks a lot

1 Like

Hey, sorry to bring this back up. I’m confused as to who is the parent and who is the child. I have both camera.go and player.go in my main.collection. I tried putting camera.go as the child (under player.go) and vice versa but my character has now disappeared. The z values for camera.go is 0.5 and player.go is 1.0 and I have tried switching the two too. Not sure what went wrong