Character centering in cameras (SOLVED)

So I’m trying to center my character in the view of a camera that follows as they move; and yes I’ve looked at previous threads, but I’m asking for help here as I still don’t understand how to position my camera properly. I’ve “childed” to the game object I want it to follow, and it does work in that sense, however my character is in the bottom left corner of the camera view.

I understand that I have to somehow offset the camera from reading other threads, but I still don’t know how to do that.

Any help is deeply appreciated, thanks in advance :slight_smile:

You can read the width and height of the screen and move the camera by half those values:

local w = tonumber(sys.get_config("display.width"))
local h = tonumber(sys.get_config("display.height"))
go.set_position(-vmath.vector3(w/2, h/2, 0), "camera")

Assuming your camera has id “camera”.

5 Likes

Hm, I tried using this but when I input this code it says vector3 value has come out as nil, would you know how to work around this possibly?

(Edit) Well it gave me that message once, now with the new code it just acts as it was before? I’m kind of lost again :frowning:

1 Like

You need to have your camera component on its own game object, and move that object. You can’t change the position of components at runtime. (The camera component doesn’t have its own position in the editor either.)

If you’re getting a nil vector, try printing out w and h to see if they are correct.

1 Like

Thank you, I’ll try this, and let you know if my issue is fixed.

1 Like

You’ll need to be using a collection file to have multiple game objects (parent-child, etc.), “.go” files are only for a single game object. Generally you should just use Collections for everything, unless it’s a really simple object like a bullet or something.

1 Like

Should the camera be having its own separate script as well? Like in the camera game object?

1 Like

No, not unless you want to do something more fancy with it. Or if you just want to keep its stuff separate—but making a separate script for three lines of code seems a bit silly.

1 Like

Okay, I’m still not sure why vector3 still comes up as a nil value? It seems like it should work, but I’m guessing w, and h can’t be found or something like that. Any ideas?

(Edit) It’s fixed, was just a small error on my end! thank you so much ^^

1 Like

Using print() is generally the simplest way to debug stuff. Before you use w and h to make the vector, just put:

print(w, h)

Or you can add extra stuff for clarity:

print("Width = ", w, "Height = ", h)

If you need to see the contents of a table, use pprint() (“pretty-print”) instead.

Check out the debugging manual.

3 Likes

Thanks, I’ll also look into this for future use :slight_smile:

2 Likes