+1 vote

I am modifying the Godot template for navigation. I want to put a camera that follows the sprite that follows the path. But at the moment I set the camera as "current" navigation begins to fail.

I intend to make a point and click game with side-scrolling.

Thats te node structure:

Navigation
   |_ path (sprite)
   |_ navpoly
   |_ agent (sprite)
        |_Camera2D

And that's the code ho extends de first node:

extends Navigation2D
# Member variables
const SPEED = 200.0

var begin = Vector2()
var end = Vector2()
var path = []


func _process(delta):
        if (path.size() > 1):
            var to_walk = delta*SPEED
            while(to_walk > 0 and path.size() >= 2):
                var pfrom = path[path.size() - 1]
                var pto = path[path.size() - 2]
                var d = pfrom.distance_to(pto)
                if (d <= to_walk):
                    path.remove(path.size() - 1)
                    to_walk -= d
                else:
                    path[path.size() - 1] = pfrom.linear_interpolate(pto, to_walk/d)
                    to_walk = 0

            var atpos = path[path.size() - 1]
            get_node("agent").set_pos(atpos)

            if (path.size() < 2):
                path = []
                set_process(false)
        else:
            set_process(false)


func _update_path():
    var p = get_simple_path(begin, end, true)
    path = Array(p) # Vector2array too complex to use, convert to regular array
    path.invert()

    set_process(true)


func _input(event):
    if (event.type == InputEvent.MOUSE_BUTTON and event.pressed and event.button_index == 1):
        begin = get_node("agent").get_pos()
        # Mouse to local navigation coordinates
        end = event.pos - get_pos()
        _update_path()


func _ready():
    set_process_input(true)
in Engine by (16 points)

it seems the problem is that only recognizes the part that is visible when loading the polygon navigation

1 Answer

+3 votes
Best answer

What's probably happening is the camera's transform is throwing off the coordinates coming from your click action.

in your _input function, change

end = event.pos - get_pos()

to

end = get_viewport_transform().affine_inverse().xform( event.global_pos )

If that doesn't work, try using pos vs global_pos, or including your - get_pos() offset.

by (334 points)
selected by

The getviewporttransform() method works perfectly! Thank you so much.

This works. Syntax has changed since 3.0, global_pos is replaced by global_position :

end = get_viewport_transform().affine_inverse().xform( event.global_position )
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.