Player movement limited to map size, not screen size

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By leggomeggo

Hi. I’m new to programming and to Godot, so I’m sorry if there is an obvious answer to this. I am trying to use the Dodge the Creeps game as a template, along with some other tutorials to accomplish what I’d like to. I am trying to make it so the player can’t leave the map, not the camera view. The map is bigger than the camera and I’ve currently got it set so the camera follows the player and stops at the edge of the map, but the player can move off the map.

I have tried the code from the Dodge the Creeps tutorial, but this makes it so the player cannot leave the camera view.

I have also tried adjusting the code used to make it so the camera can’t leave the edge, but I keep getting different errors about that one.

I’ve tried Googling and watching tutorial videos and I can’t seem to find anything besides the clamp code below.

Here is the code I tried to use from the Dodge the Creeps tutorial:

position += velocity * delta
position.x = clamp(position.x, 0, screensize.x)
position.y = clamp(position.y, 0, screensize.y)

I’m not sure if anymore of my code is needed? I am following the KidsCanCode Top-down Tank Battle tutorial for movement and reference, but ultimately am trying to make a customized version of Asteroids.

Thank you!

:bust_in_silhouette: Reply From: kidscancode

If you want to limit your player’s movement to the map, you need to use the map coordinates in the clamp() function, not the screen size. The same values you’re using to set the camera limits.

You could even pull them from the camera:

position.x = clamp(position.x, $Camera.limit_left, $Camera.limit_right)
position.y = clamp(position.y, $Camera.limit_top, $Camera.limit_bottom)

Thank you!

I am having trouble now because I have variables (I think that’s the correct terminology for what I’m trying to say) from multiple scripts that I’m trying to use for this.

For example, I tried placing your code into my with my camera limit, as I need the map_limits variable I have set. This works fine, but just placing your code, with no other code, I can still leave the map. I have my player instanced in my map scene, but I’m guessing this isn’t enough to use your code?

I tried placing

position += velocity * delta

along with the code, which of course throws an error because velocity and delta aren’t defined. I’ve tried placing all code into the script, which throws a null instance error of limit_left because I’m trying to reference the script (didn’t think that would work, but figured I’d try).

I understand why the code isn’t working for the most part, just still not totally sure how to make it work. I am following your top-down tanks tutorial, so I have in place of your and is extending from (, if that helps to paint you a picture.

Thank you for your very fast response, you’ve gotten me a step forward!

leggomeggo | 2018-04-15 18:18

Well, you wouldn’t put that code in the map, because it’s not the map’s position you’re trying to limit (and the map doesn’t have a velocity).

Think of it this way: when you add a node, it has a bunch of different properties and functions that come along with it. A Node2D has a position for example.

When you attach a script to a node, what you are doing is adding additional custom properties and functions to that node, which is why the script starts with extends.

Now you can technically put code on any node, and use get_node() to find another node and access its properties, but this is usually a good way to make a bunch of messy and unorganized code.

For example, in the map, you can access the player via $Player.position, etc. But as you can see in the example above, I didn’t use any map properties or variables - I used the player’s own camera node’s properties. The code I gave you would go in the player’s _physics_process() method, which also makes sense since that’s where you’re moving the player (changing its position). In general, you should try to put player code on the player, map code on the map, etc. If the map needs to tell the player something, like how big it is, you can do that like we do in the map’s _ready().

One more piece of advice: be careful you’re not trying to do too many things at once. In my experience this is a very common pitfall for beginners. If you notice when following my videos, I add one thing at a time. When that thing works, I move on to the next. When following a tutorial it’s easy to just parrot what’s on screen and keep going. Don’t move on until you are confident that you understand what you just did.

kidscancode | 2018-04-15 18:42

Thank you so much, it’s working perfectly!

I think I was getting a bit ahead of myself and agree that I need to slow down, thank you for your advice!

leggomeggo | 2018-04-15 19:25