What I'm doing wrong with Navigation and Camera

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By punkomalo
:warning: Old Version Published before Godot 3 was released.

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)

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

punkomalo | 2016-04-20 13:44

:bust_in_silhouette: Reply From: jackmakesthings

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.

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

punkomalo | 2016-04-21 06:16

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 )

Flying_Dindoleon | 2018-03-28 11:28