|
|
|
|
Reply From: |
Kamil Lewan |
Maybe Path2D
/Curve2D
'll help you. After pathfinding (by a* or custom pathfinder), you should have var with list of vectors with position of each tile(?) or point (in global, or
fictitious units (like tiles), if 2nd option, you must multiple vector in set_pos()
by size of tile).
When you have it, you should do (in animation, or (i think better) in _process()
an animation.
If it don’t work on tiles, you may use Path2D and get_baked_points()
, like
_process(delta):
set_pos(your_path_2d.get_baked_points()[number_of_frame])
EDIT: Final code:
GitHub demo
Map code:
tool
extends Polygon2D
# map size in pxs
export var map_size_x = 500
export var map_size_y = 500
# move map from left top corner
export var init_pos_x = 150
export var init_pos_y = 150
func _ready():
# create polygon
set_pos(Vector2(init_pos_x,init_pos_y))
set_polygon(Vector2Array([
Vector2(0,0),
Vector2(map_size_x,0),
Vector2(map_size_x,map_size_y),
Vector2(0,map_size_y)]
))
# move Nav2D poly to true pos (with position of map node)
var _buff_poly = Vector2Array([])
for i in get_polygon():
_buff_poly.append(i+get_pos())
# set polygon to Nav2D
var _nav_poly = NavigationPolygon.new()
_nav_poly.add_outline(_buff_poly)
_nav_poly.make_polygons_from_outlines()
get_node(\"Navigation2D\").navpoly_create(_nav_poly,Matrix32(0,Vector2(1,1)))
Unit code:
extends Sprite
var movement_path = Curve2D.new()
var speed = 250.0
var pos_on_path = 0
func _ready():
# that's in pixels (probably)
movement_path.set_bake_interval(1)
set_process_unhandled_input(true)
set_fixed_process(true)
func _unhandled_input(event):
if event.type == InputEvent.MOUSE_BUTTON and event.pressed:
if event.button_index == BUTTON_RIGHT:
movement_path.clear_points()
movement_path.add_point(get_pos())
movement_path.add_point(event.global_pos)
var _buffer = get_node(\"../A passable map/Navigation2D\").get_simple_path(movement_path.get_point_pos(0),movement_path.get_point_pos(1))
movement_path.clear_points()
for i in _buffer:
movement_path.add_point(i)
pos_on_path = 0
func _fixed_process(delta):
if round(pos_on_path+speed*delta) < movement_path.get_baked_points().size():
pos_on_path += speed*delta
set_pos(movement_path.get_baked_points()[round(pos_on_path)])
Thanks for your reply!
If tried using the function get_baked_points
, since I m not using tilemaps.
but it keep giving me an error.
I have the following:
func _fixed_process(delta):
if get_node("../Navigation2D").path.size():
set_pos(get_node("../Navigation2D").path.get_baked_points())
I get the errror:
Invalid call. Nonexistent function ‘get_baked_points
’ in base
‘Vector2Array’.
I have printed the path it does return some values I have an example at the bottom)
What do you mean by [number_of_frame], am I missing something?
if I print get_node("../Navigation2D").path
I receive:
[(81.000305, 125.136993), (413, 382)]
Santouryuu | 2017-06-21 10:09
The problem is get_baked_points
return array of points, so you can’t just use set_pos()
. You must use set_pos(get_baked_points()[a])
. That a
is number of backed point, like pixel by pixel. So if you want move 1px/frame you should have something like this:
act_pos_in_array = 0
func _process(delta):
set_pos(get_baked_points()[act_pos_in_array])
if act_pos_in_array < get_baked_points().size()
act_pos_in_array += 1
Other thing, what’s path
in your code?
Kamil Lewan | 2017-06-23 17:04
First of all thanks for sticking with me!
And I Apologize for my late reply.
I kinda got it to work sprite moves now but not in a smooth motion.
It goes to the first point than waits a little then goes to the next and again halts for a fraction of a second.
func _input(event):
if event.type == InputEvent.MOUSE_BUTTON and event.pressed:
if event.button_index == BUTTON_RIGHT:
end = event.global_pos
start = get_node("Tank").get_pos()
transition = 0
func _process(delta):
path = get_node("Navigation2D").get_simple_path(start, end)
if path.size() < path_points:
transition = 0
path_points = path.size()
if path_points < 2: return
var sprite = get_node("Tank")
sprite.set_global_pos(sprite.get_global_pos().linear_interpolate(path[1], transition))
start = sprite.get_global_pos()
transition += speed * delta
if transition > 1: transition = 0
This is currently the code.
Tank is a rigidbody2d object.
I would very much appreciate it if you were able to tell me a way to make the motion smoother
Santouryuu | 2017-07-03 13:52
I think, i can’t explain that easly. I pushed demo project to GitHub, hope it will be helpful.
It’s not best way to do that, but it works, so it’s good to prototyping…
Kamil Lewan | 2017-07-04 11:36
Again thank you very much!!
At least I get the logic behind it now.
Cheers buddy
Santouryuu | 2017-07-04 13:28