|
|
|
|
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 map.gd 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 player.gd script, which throws a null instance error of limit_left because I’m trying to reference the map.gd 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 Ship.gd in place of your Tank.gd and Player.gd is extending from Ship.gd (Tank.gd), 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