Hello I’m trying to make a top down dungeon shooter and I want the player projectiles to go towards wherever the player clicks with their mouse. I have a camera object that follows the player game object. When I make a mouse click the location doesn’t seem to be where I actually click but rather in the same location but in a screen sized area around the centre of the map. I would greatly appreciate a pointer about what is going on here.
You need to translate the screen position to your world position. You can see an example of this here:
Hello. I’ve been very busy recently and haven’t been able to try this until now. This doesn’t seem to work and seems to teleport the target away from the start when I move the mouse like it did previously
What is the target? I thought this was about getting the world position of a mouse click and then spawning a bullet at the player position and moving it to the world position of the mouse click?
Yes, sorry for not being more clear. Having the player fire towards the click is my ultimate goal but I currently have a game object for the cursor and have attached a crosshair/target sprite to it so it follows where the player is aiming ideally. This target isn’t a necessity of course.
And what is the problem?
After putting the script you linked to on the cursor game object the target doesn’t follow where I move the mouse or where I click.
Impossible to say what is wrong. Are you able to share the project here?
Yes, there’s a few things you need to change:
- Tell the render script to use the camera projection in your camera.script:
function init(self)
msg.post("#camera1", "acquire_camera_focus") -- acquire camera focus for the camera component
msg.post("@render:", "use_camera_projection")
end
- Modify your camera component so that the Orthographic Projection checkbox is checked and set Near Z to -10 and Far Z to 10.
- Do not offset the camera when you parent it to the player:
function on_message(self, message_id, message, sender)
if message_id == hash("follow") then -- start following the player object that sent the message...
go.set_parent(".", sender) -- ... by parenting the player
--go.set_position(vmath.vector3(-1000, -700, 0)) -- offset the camera so it is centered on the player
Thank you. Removing the offsetting line actually ended up putting the camera in the wrong place but setting all the values to 0 has fixed this.