How can i make the Camera Follow the player?

I realize this is kind of a stupid question, but i’m still new to scripting in general, in fact i’ve never even finished a single Lua tutorial. And i’m also using the Rendercam plugin, which has a follow option built in. I just can’t figure out how to make it work

Any help would be much appreciated. Thank you for reading

What did you try and how did it not work? Are you making a 2D game? (Rendercam’s follow feature only works on the X-Y plane.)

Yes i’m making a 2d game. I tried making the camera that’s attached to the player and also linked with rendercam use the basic follow script from the Defold examples

It didn’t work

-How- did it not work? What happened? Did you get an error? Did it crash? I need a lot more information to be able to guess what went wrong and help you. Sharing your code would help, or zipping up your project folder and attaching it here (if the project is small).

Sorry. I got an error. I’ve also since deleted that section of the script because i was trying to test something else and i thought it might have been that part of the script that was stoping me from running the game. I thought to myself i could rebuild the camera function later

I am rewriting it right now. to see if i still have the same problem

This is what i’m using in the player script to make the camera follow the player:

local speed = 100

local rendercam = require “rendercam.rendercam”

function init(self)
msg.post(".", “acquire_input_focus”)
self.dir = vmath.vector3()
self.current_anim = nil
msg.post(“camera”, “follow”)
self.follow = true
end

But what i don’t know is. A: Will that even work. and B: What will i have to do in rendercams camera script to make the camera follow the player. If that part of my script even works.

At the moment, the Rendercam camera doesn’t recognize a “follow” message, you use the function rendercam.follow(target_id) (documentation).

One caveat is: The init order of objects in the same collection is undefined, and the camera can’t get commands until after it is init. This is pretty easy to get around with timer.delay though.
Some example code:

local rendercam = require "rendercam.rendercam"

function init(self)
	local myID = go.get_id()
	timer.delay(0, false, function() rendercam.follow(myID) end)
	-- A timer with a delay of zero will trigger just before the text frame's update.
end

That’s all you have to do. You shouldn’t have to modify Rendercam code (or the code of any extension, usually).


If Rendercam is missing some features that you want, take a look at Orthographic, it’s actively maintained and has more features than Rendercam.

2 Likes

Than you so much :grinning:

1 Like

Cool thanks!

1 Like