Camera Trouble

I’ve tried to look in other topics, but I have connected a camera to a moving sprite and the orthographic projection isn’t working, every time I build the project just black comes up. This is the code for the sprite :

function init(self)
	msg.post("camera#Camera", "acquire_camera_focus")
	msg.post(".", "acquire_input_focus")
	msg.post("#", "menu")
	self.play = 0
	-- 0 = none; 1 = play; 2 = win; 3 = lose; 4 = game
	self.timer = 0
	self.game = 0
end

function update(self, dt)
	if (self.play == 1) then
		go.set_position(vmath.vector3(560, 970, 0))
		local timer = self.timer
		self.timer = timer + 1
		if (self.timer == 600) then
			msg.post("enemy#enemy", "play")
			self.timer = 0
			self.play = 4
			msg.post("enemy#enemy", "play")
			msg.post("player#player", "play")
		end
	end
	if (self.play == 2) then
		go.set_position(vmath.vector3(560, 970, 0))
		local timer = self.timer
		self.timer = timer + 1
		if (self.timer == 600) then
			self.timer = 0
			go.set_position(vmath.vector3(560, 970, 0))
		end
	end
	if (self.play == 3) then
		go.set_position(vmath.vector3(560, 970, 0))
		local timer = self.timer
		self.timer = timer + 1
		if (self.timer == 600) then
			self.timer = 0
			go.set_position(vmath.vector3(560, 970, 0))
		end
	end
	if (self.play == 4) then
		go.set_position(go.get_position("player"))
		if (pos.x > 34) then
			pos.x = 40
		end
		if (pos.x < 34) then
			pos.x = 40
		end
		if (pos.y > 34) then
			pos.y = 40
		end
		if (pos.y < 34) then
			pos.y = 40
		end
	end
end

function on_message(self, message_id, message, sender)
	if (message.id == hash("play")) then
		self.play = 1
	end
	if (message.id == hash("dates")) then
		go.set_position(vmath.vector3(560, 970, 0))
	end
	if (message.id == hash("win")) then
		self.play = 2
	end
	if (message.id == hash("lose")) then
		self.play = 3
	end
	if (message.id == hash("menu")) then
		go.set_position(vmath.vector3(560, 970, 0))
	end
end

A lot of the placements are just copy and paste, and will eventually change.

Where is this script attached? And where is the camera positioned? And the player?

When the camera gets focus it will send it’s view projection to the render script which will render the specified area. Is your game objects positioned where the camera is placed?

The script is attached to a game object that has the script, the object for the camera only, and the sprite I want the camera to follow. I haven’t made a render script and am still confused toward cameras, after reading the manuals on the subject multiple times.

Please post screenshots of your setup.

In order to figure out what’s wrong we need to be able to recreate your setup and that is currently not possible. We need to know what the game objects’ positions are, how they are childed to each other (if so) and their ids, since the script references those.


I just found out that changing the z placement results in different things being shown

Your camera is at Z position 10. The default render script renders from -1 to 1 (relative to the camera projection) so anything that is positioned outside the 9-11 range will not be rendered at all.

1 Like

Put the camera at Z 0 and make sure that everything is within -1 and 1, or change the render script.

3 Likes

Thanks!! that worked after placing all z positions in the domain [-1, 1]

Now the camera, no matter where the game object is will show the same spot.
Here’s the new script :

 function init(self)
	msg.post(".", "acquire_camera_focus")
	msg.post(".", "acquire_import_focus")
	msg.post(".", "menu")
	self.play = 0
	-- 0 = none; 1 = rules; 2 = win; 3 = lose; 4 = game; 5 = credits; 6 = menu
	-- 7 = dates
	self.timer = 0
	go.set_position(vmath.vector3(-684, -260, 0))
end

function update(self, dt)
	if (self.play == 1) then
		go.set_position(vmath.vector3(1244, -200, 0))
		local timer = self.timer
		self.timer = timer + 1
		if (self.timer == 600) then
			self.timer = 0
			self.play = 4
			msg.post("enemy#enemy", "play")
			msg.post("player#player", "play")
		end
	end
	if (self.play == 2) then
		go.set_position(vmath.vector3(684, 260, 0))
		local timer = self.timer
		self.timer = timer + 1
		if (self.timer == 600) then
			self.timer = 0
			self.play = 5
		end
	end
	if (self.play == 3) then
		go.set_position(vmath.vector3(684, -260, 0))
		local timer = self.timer
		self.timer = timer + 1
		if (self.timer == 600) then
			self.timer = 0
			self.play = 5
		end
	end
	if (self.play == 4) then
		local vec = go.set_position(go.get_position("player"))
		local x = vec.x
		local y = vec.y
		local z = vec.z
		if (vec.x > 119) then
			x = 120
		end
		if (vec.x < -119) then
			x = -120
		end
		if (vec.y > 259) then
			y = 260
		end
		if (vec.y < -259) then
			y = -260
		end
		go.set_position(vmath.vector3(x, y, z))
	end
	if (self.play == 5) then
		go.set_position(vmath.vector3(1244, 200, 0))
		local timer = self.timer
		self.timer = timer + 1
		if (self.timer == 600) then
			self.timer = 0
			self.play = 6
		end
	end
	if (self.play == 6) then
		go.set_position(vmath.vector3(-684, -260, 0))
	end
	if (self.play == 7) then
		go.set_position(vmath.vector3(-684, 260, 0))
	end
end

function on_message(self, message_id, message, sender)
	if (message.id == hash("play")) then
		self.play = 1
	end
	if (message.id == hash("dates")) then
		self.play = 7
	end
	if (message.id == hash("win")) then
		self.play = 2
	end
	if (message.id == hash("lose")) then
		self.play = 3
	end
	if (message.id == hash("menu")) then
		self.play = 6
	end
end

2018-06-07%20(1)

This image seems to show part of the problem:
49

Try this:

  1. Put this code into “camera.script” to make sure that it’s the camera that acquires the camera focus.

msg.post("#Camera", “acquire_camera_focus”)

  1. Put the Camera (the one that has the camera icon) into the same Game Object as that script.

  2. It should look like this (although your filenames may be different):

38

I added the msg.post, and moved the script to the camera , vice versa, stuck everything in on folder, and then in other and yet the same problem keeps happening. Can I get some help? Should the sprite, according to the example, be in the GOCamera object?

The “acquire_camera_focus” message should be sent to the camera component. Once this message is sent the camera will automatically and each frame send it’s update view projection to the render script and essentially follow the game object it is attached to.

The problem you are having is that you are not sending the “acquire_camera_focus” message to the camera component and that is why it is stuck in the same place.

Please make sure that the camera component is on the same game object as the script that does this:

msg.post(".", "acquire_camera_focus")

The line of code above will send the “acquire_camera_focus” message to all components on the same game object as the script. If your camera component has id “camera” then you could also send the message explicitly to the camera component instead of sending it to all components on the game object:

msg.post("#camera", "acquire_camera_focus")

If the script and the camera component are NOT on the same game object then you need to send the acquire_camera_focus message to the game object with the camera component. If the script is on one game object named “B” and the camera component is on another game object named “A” then you need to send a message like this:

msg.post("A", "acquire_camera_focus")

Or more specific to the camera component only:

msg.post("A#camera", "acquire_camera_focus")

This follows the basic rules of message passing in Defold. Read more about this here: https://www.defold.com/manuals/message-passing/

1 Like