Movin the map around the player.

JohynJohyn Posts: 48Member
edited March 29 in 2D

Greets!

Here's the jigsaw: keepin my player sprite pinned at the center of my mini map, with the map movin around him.
Have a mouse script for that, on a kinematicbody with the map/sprite:

Notice the - before mouse_pos in tween part. Put it there as the 'normal' script was without, and was workin quite fine, but for the inversed direction of the movement. A right click was goin left, and inversely.

So tried that -, but what happen now is that on the first click, wherever it is, the map goes straight to the upper left corner... And I don't know how to center all that...

So if you have an hint on the how to solve that, it would be much apreciated. :)

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 784Admin

    Because it goes to the upper left corner, I would say one of your vectors starts at (0, 0), since that is the coordinates of the top left corner and (0, 0) is the default value for any newly constructed Vector2s.

    I would suggest printing all of your vector variables to see which is causing the problem. One of the vectors should (if my theory is correct) return with a value of (0, 0).

  • JohynJohyn Posts: 48Member

    Erh.. Got no vector variables eh. All the scene is here.

  • TwistedTwiglegTwistedTwigleg Posts: 784Admin

    Sorry, I should have clarified.

    What I meant is that you should print $Map/Sprite.global_position and get_global_mouse_position and see what they return on the first click.

  • JohynJohyn Posts: 48Member

    Sorry, don't know how to just print that... :s Tried the 'print' in all manners of syntax, to get an 'unexpected token: built in func'.

  • TwistedTwiglegTwistedTwigleg Posts: 784Admin

    No problem! Using the code you posted, it should look like this:

    extends Node2D
    var terrain_speed = 1.0
    var speed = 50
    func _input(event):
        if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
            var map_pos = $Map/Sprite.global_position
            var mouse_pos = get_global_mouse_position()
            "There are two ways to print the positions:"
            "1. Print the values by printing the variables"
            print(map_pos)
            "2. Print the values by grabbing them, like when we set the variables"
            print(get_global_mouse_position())
            "... (Paste the rest of the code below ) "

    As I wrote in the comments, you can either print the variable or print the value directly. Either way will work, though since you have the variables I'd just print those.

  • JohynJohyn Posts: 48Member

    That's exactly what I wrote! without success... But maybe cause that was at the end of my code, after the tween part?
    Ok, here's the output:
    There are the mouse coordinates, and a 0.0 position...

  • TwistedTwiglegTwistedTwigleg Posts: 784Admin

    Which one is (0, 0) position? the mouse coordinates or map_pos?

  • JohynJohyn Posts: 48Member

    The mouse coordinates should be the ones printed between () no? But I don't know from where comes those 0.0...

  • TwistedTwiglegTwistedTwigleg Posts: 784Admin

    Assuming you are using print(get_global_mouse_position) or print(mouse_pos), then yeah :smile:

    But we need to know the other position, map_pos as well (which in this case seems to be the culprit). Can you add print(map_pos) under the print statement that prints the mouse?

  • JohynJohyn Posts: 48Member

    Sry, t'was the map_pos coordinates before. Now, for mouse_pos, they are 1287.643, wich are located in my mini map when clickin, right?

  • TwistedTwiglegTwistedTwigleg Posts: 784Admin

    they are 1287.643, wich are located in my mini map when clickin, right?

    Yup!

    Unfortunately this means its something else that is the problem, and I'm not sure what it could be. However, I do have a (potential) messy fix: Because it's only the first click that causes issues, just ignore the first click (and resend it)

    It would look like this:

    extends Node2D
        var terrain_speed = 1.0
        var speed = 50
        var first_input = false
        func _input(event):
            if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
                if first_input == false:
                    first_input = true
            " Not sure if this will work."
                    Input.parse_input_event(event)
                    return
                var map_pos = $Map/Sprite.global_position
                var mouse_pos = get_global_mouse_position()
                var distance = map_pos.distance_to(mouse_pos)
                var duration = distance * terrain_speed/speed
               " The picture does not show the tween calls fully, so I cannot write them here "

    If sending the input event again does not work, then you could try changing it to this $Map/Sprite.global_position = get_global_mouse_position (just remove Input.parse_input_event(event) and replace it with the code)

    Other than that, I'm not sure. I can try to debug the project if you zip it and upload it here, but without looking at the project I cannot say what the problem is, because I just don't know enough about 2D nodes / Godot in 2D.

    Hopefully you'll find a solution (or someone who knows better than me will chime in :smile: )

  • JohynJohyn Posts: 48Member

    Ok, corected a few lines indents, but the scene can't start cause now it doesn't find the node Map/sprite. Can't see why, as it saw it before... See my code:
    And here's the project, mostly graphics and lil animations as I'm still makin my first steps: http://dl.free.fr/jVddSl9fI Can't believe it's already 2.5GO, in ZIP! While it's but images and sounds. Look for Map.tscn if you got time and will for that, cheers!
    Got another helper for that, see what he says: https://godotengine.org/qa/26486/makin-the-map-movin-around-my-player?show=26547#c26547

  • JohynJohyn Posts: 48Member
    Answer ✓

    Ok, it's done thxs to the skill of Bartosz. Here's its code:

Sign In or Register to comment.