Help understanding how to move a Camera

#1

Hello Defold people,

I need help understanding how to properly move a camera. I am still very green with defold and coding. I am trying to move the camera instead of moving the entire background and all the go’s(which works but wouldn’t it be easier on rendering if I just move the camera?). When I move the camera using “aquire_camera_focus”, I cannot click on my character to move him again? Also, what is the best way to get two scripts to talk to each other?

Sorry, I am still new to all this, I appreciate all of your patience. :slight_smile:

0 Likes

#2

For cameras it would be much easier if you used something like Orthographic or RenderCam instead of doing the harder bits by yourself.

You can have scripts send each other messages with msg.post() or you can use a Lua module that both require and share data that way.

1 Like

#3

Did you check the Camera example?

2 Likes

#4

Thanks guys,

That helps a ton and I will check out the camera example.

0 Likes

#5

Hello again,

I started using the orthographic camera you had suggested and when I add it to my dependencies and render script I can no longer click on the go I created to click somewhere else to move him. Then I remove the orthographic camera from the collection and default the render script and it starts running properly. What is the reason it is doing this? Please let me know what else you need from me to help me out.

Thanks

0 Likes

#6

You most likely need to filter the input

1 Like

#7

What does your input code look like to detect the click?

1 Like

#8

This passes over to another script I have that then moves the orthographic/camera/.go

if action_id == hash(“down”) then
msg.post("/camera#script", “down”)
elseif action_id == hash(“up”) then
msg.post("/camera#script", “up”)
elseif action_id == hash(“right”) then
msg.post("/camera#script", “right”)
elseif action_id == hash(“left”) then
msg.post("/camera#script", “left”)
end

0 Likes

#9

Ok. And you have checked that the /camera#script gets these messages? And handles them properly? What about the camera script from orthographic? How does it relate to the script you are sending messages to?

Zip the project and share it here please.

1 Like

#10

PIXnD.zip (515.4 KB)

0 Likes

#11

I took a look at your project and here’s some conclusions:

  • main.script is attached to the knight game object
  • main.script acquires input focus
  • main.script handles input triggers for down, up, right and left properly
  • main.script posts as message to /camera#script with an action corresponding to the key that was pressed
  • /camera#script is the camera.script from Orthographic
  • camera.script has no logic to deal with the messages sent from main.script

You can’t modify camera.script since it is part of the library project. You can make temporary changes in the editor but they won’t save. Here’s what I suggest you do:

  • Keep the input handling in main.script and use it to move the knight game object
  • On the camera.script on camera.go you should check the Follow checkbox and set Follow Target to /knight to have the camera follow the knight game object as it moves around
1 Like

#12

Thank you so much man. So would it better for me to move the code that was on the selected script to the main script?

I added the correct logic to move around the camera go so I can move the camera now, which is great but I still cannot click on the knight for whatever reason?

0 Likes

#13

Hi again,

I think it is something with the camera coordinates. I have the knight set to ( 0, 0, 0 ) and the bottom of the window when I run the project is displaying as ( 0, 0, 0 ). So when I click on that part of the screen my character lights up.

So, how do I get the camera to use the world coordinates and not the window coordinates?

Thank you for your help

0 Likes

#14

Mouse coordinates will always be in screen space, meaning that they range from 0x0 to screen width X height. The camera provides a camera.screen_to_world() function to convert from screen coordinates to world space based on the current position and properties of the camera. Check the Orthographic camera docs for the available API functions.

1 Like

#15

Awesome! This may make you face palm but how to I use this function? I cant figure out how to use it in my other scripts. thanks again

0 Likes

#16

Did you look at the example that comes with the extension? You can for instance see it used here:

1 Like