Camera lock on x-axis? (SOLVED)

I am trying to lock my camera on the x-axis, i.e. the camera will only change when i move left, right, down and up. If i jump, i don’t want the camera to move. I saw this question locking camera and i tried to fiddle with the code, but i couldn’t produce what i want.

Actually, i was able to do it. First, the camera game object must not be nested inside the player object. The tracking of the camera must be done from code side.

I have created a camera script, and inside the update method, i update only the x-axis and leave the y-axis the same. Below is the code. Now, i am facing another problem that i am trying to get around. I have a ladder, and once i start descending or ascending, the y-axis must be updated. I have added a on_message method to posted a message to the camera script. All is fine, but once i start updating the camera y-axis, everything gets messed up, because there is a big difference between camera and player positions.

local msg_follow_yAxis = hash("followY")

function init(self)
	msg.post("#camera", "acquire_camera_focus")
end

function update(self, dt)
	msg.post("@render:", "clear_color", { color = vmath.vector4(0.2, 0.2, 0.2, 1) })
	
	local player = go.get_position("player")
	local camera = go.get_position("camera")
	
	go.set_position(vmath.vector3(player.x, camera.y, 0), "camera")
end

function on_message(self, message_id, message, sender)
if message_id == msg_follow_yAxis then    
    local player = go.get_position("player")
		local camera = go.get_position("camera")

		camera.y = player.y - 1
		go.set_position(vmath.vector3(player.x, camera.y, 0), "camera")
  end
end
1 Like

Good that you solved it yourself! An alternative to your approach could be to use my orthographic camera library. It allows you to define a “deadzone”, ie an area around the player where the camera will not follow.

local camera = require "orthographic.camera"    
camera.deadzone(camera_id, left, top, right, bottom)

Thanks for the suggestion. I have tried implanting it, but all i am getting is black screen. What i did is i extracted the package, and took the folder “orgthographic” and put it inside he “main” folder. I had some previous camera objects and scrips, so i removed everything. I added the camera.go object to the root of my game.collection. I made that the script in the camera.go is referenced correctly. I have used the below command in my player.script in the init() method:
msg.post("camera", "follow", { target = "player"})

I build and ran, and all i got was a black screen. Am i doing anything wrong, or am i missing something?

A much better way of using the library is to open game.project and in the Dependencies field add:

https://github.com/britzl/defold-orthographic/archive/master.zip

When you’ve added the dependency you can do Project->Fetch Libraries to get the project included as a read-only library (same as the builtins folder).

Once you’ve added the camera.go to your collection you should also double check that it’s enabled and that the near and far z-value is within the range you need. Select the script on the camera.go and make sure Enabled is checked and that Near-z and Far-z are ok (default is 1 and -1).

Does the camera work before you send the “follow” message? Do you see anything in the log? Is the id of the player game object actually “player”?

Are you using a custom render script? You should make sure that you are handling the view and projection sent from the camera component. The projection is handled in the render script by the on_message() function and the set_view_projection message (same as the built-in component). The difference is that the default render script ignores the view and only uses the projection. You should check the Render Script Integration section of the readme or look at the example render script.

1 Like

I was able to run he library. The problem is that i had a a render script, so i changed to the default one and everything was fine. After that, to achieve the zoom, i used the a custom script as in one of the examples, and everything is working fine.

Now i want to achieve the following, but i am really not sure where to start. I will put some screen shots. In the screen shots below, i think the zoom is good, but there are lots of black spaces. As you can see, that the level moves down as in the third picture, which means i need to remove the black spaces from all directors. Is this achievable or am i doing something fundamentally wrong here?

What is the expected result? Do you want to zoom in more? Or replace the black with some other color? Or show more of the level?

Thank you very much for the great support.

You seem to have listed all the available options for me. I guess i was thinking that zooming in more would ruin the graphics, and that is why i thought that maybe i could crop the black areas somehow.

Edit:
I have asked a lot of questions. The most important question is this:
*Lets say that i zoomed-in by 2X, and i have fixed my level design such that i cover all black areas. What happen if i increase or lower the resolution. I have lowered my resolution to 640x480 and the level design was cropped and the zoom increases. How can i fix these kind of issues?

This depends on your graphics and the style. If you’re going for a retro 8/16 bit style then zooming in shouldn’t be a problem. If you’re going for crisp graphics of a contemporary style then I’d say that the recommendation is to think of the graphics that you are using and if/how to zoom it. Background images can probably be zoomed in depending on art style. Sprites and tilemaps should probably be drawn in the correct size and not zoomed.

What do you mean by “lower the resolution”? Lower the resolution of the monitor/display or reduced the size of the window in game.project?

The value in game.project defines the initial window size in a desktop build. If the window is resized it depends on your render script what will happen. Should the content be scaled (ie zoomed) up/down or should you show more/less of the game content? You also need to have a render script that handles a different aspect ratio than the initial aspect ratio. If the render script doesn’t handle this properly you end up with stretched graphics.

You really need to think about how you wish to present your game. Look to similar games and see how they solve the problem. Can you do the same?

Thank you @britzl for your reply. It took me sometime, but i did some reading and i understand the concept now. I was writing my own code, then i have seen the code in one of your examples and made some tweaking to achieve my goals. All is working now on different aspect ratios. Thank you for your support.

2 Likes