I'm making a delivery system, and the question is, why does it only come to pos11?

Godot Version

Godot 4.4.1

Question

I’m confused, it should work. Can you tell me how to fix it, or show me a correct example?

	if SD:
		var to_two = false
		if global_position.distance_to(pos11) <= 0.002 and !to_two:
			set_target_position(pos22)
			to_two = true
		if global_position.distance_to(pos22) <= 0.002 and to_two:
			set_target_position(pos11)
			to_two = false
func sbor_dani(pos1 : Vector3, pos2 : Vector3, unit : CharacterBody3D):
	on_sbordany_func = true
	pos11 = pos1
	pos22 = pos2
	fuck_unit = unit
	set_target_position(pos11)
	SD = true

What’s wrong with it? What do you expect to happen versus what really happens?

1 Like

It should go to point 1 and then to point 2. But it only goes to point 1 and keeps going. But it only goes to point 1 because of the direct call from the function (code number 2), but it should move because of the first piece of code.

I think I explained it very confusing.

I assume you’re trying to make something move back and forth between two points. Your to_two var is going to start false every time though that code, and your distance check is only going to work if you wind up almost exactly on the position; if you pass through it at speed, you may not be “on” the point when you’re checking distance.

I’d suggest something like:

# Untested...

var patrol_waypoints:      Array = []
var patrol_pingpong:       bool
var patrol_loop_forward:   bool        # true -> forward, false -> reverse
var patrol_index:          int
var patrol_unit:           CharacterBody3D
var patrol_current_target: Vector3
var patrol_waypoint_range: float

func setup_patrol(unit: CharacterBody3D, pingpong: bool, points: Array):
    patrol_waypoints      = points
    patrol_pingpong       = pingpong
    patrol_loop_forward   = true
    patrol_index          = 0
    patrol_unit           = unit
    patrol_current_target = patrol_waypoints[0]
    patrol_waypoint_range = patrol_unit.global_position.distance_to(patrol_current_waypoint)

func update_patrol(delta: float):
    var cur_dist = patrol_unit.global_position.distance_to(patrol_current_waypoint)

    #    If we're farther than we were, we've reached it and overshot slightly, so we 
    # target the next waypoint.
    if cur_dist > patrol_waypoint_range || cur_dist < 0.002:
        if patrol_loop_forward: # Traverse forwards.
            if patrol_index < (patrol_waypoints.size() - 1): # Mid-loop, go forwards.
                patrol_index += 1
            elif patrol_pingpong: # End & pingpong, reverse.
                patrol_index -= 1
                patrol_loop_forward = false
            else: # End & not pingpong, wrap.
                patrol_index = 0
        else: # Traverse backwards.
            if patrol_index > 0: # Mid loop, go backwards.
                patrol_index -= 1
            elif patrol_pingpong: # End & pingpong, go forwards.
                patrol_loop_forward = true
                patrol_index += 1
            else: # End & not pingpong, wrap.
                patrol_index = patrol_waypoints.size() - 1

        # Set up the new point.
        patrol_current_target = patrol_waypoints[patrol_index]
        patrol_waypoint_range = patrol_unit.global_position.distance_to(patrol_current_waypoint)

func _process(delta: float):
    [...stuff...]

    # Do patrol things.
    if is_instance_valid(patrol_unit):
        update_patrol(delta)

That may be more complex than what you want; it supports an arbitrary number of patrol points, and either cycling through the points or pingponging back and forth from one end to the other.

For distance checks, we keep the 0.002 range check, but we add another check to see if we’ve got further away; we treat that as an overshoot and move to the next waypoint.

2 Likes

It looks interesting, but it’s complicated. I think I can figure it out in a week. Thank you!

How do you store your target position? How is the functionset_target_position defined? Could you check distance to that target position instead?

var pos11: Vector3
var pos22: Vector3
var current_target: Vector3

if global_position.distance_to(current_target) <= 0.02:
    # switch target
    if current_target == pos11:
        current_target = pos22
    else:
        current_target = pos11
1 Like

Here’s a simpler version that doesn’t do pingpong or reverse loops:

var patrol_waypoints:      Array = []
var patrol_index:          int
var patrol_unit:           CharacterBody3D
var patrol_current_target: Vector3
var patrol_waypoint_range: float

func setup_patrol(unit: CharacterBody3D, points: Array):
    patrol_waypoints      = points
    patrol_index          = 0
    patrol_unit           = unit
    patrol_current_target = patrol_waypoints[0]
    patrol_waypoint_range = patrol_unit.global_position.distance_to(patrol_current_waypoint)

func update_patrol(delta: float):
    var cur_dist = patrol_unit.global_position.distance_to(patrol_current_waypoint)

    #    If we're farther than we were, we've reached it and overshot slightly, so we 
    # target the next waypoint.
    if cur_dist > patrol_waypoint_range || cur_dist < 0.002:
        if patrol_index < (patrol_waypoints.size() - 1): # Mid-loop, go forwards.
            patrol_index += 1
        else: # End.  Wrap.
            patrol_index = 0

        # Set up the new point.
        patrol_current_target = patrol_waypoints[patrol_index]
        patrol_waypoint_range = patrol_unit.global_position.distance_to(patrol_current_waypoint)

func _process(delta: float):
    [...stuff...]

    # Do patrol things.
    if is_instance_valid(patrol_unit):
        update_patrol(delta)
1 Like

Not working :cry:

Thank you, but I don’t have less time to study.

It’s an example, not complete code. If you wanted to use it you may have to change your set_target_position function

1 Like

I know. I’m not very good at coding, but I have a d20 wisdom (that was a very lame joke)

1 Like

Godot has str and int, but not wis, dex, con or cha.

2 Likes

Godot Engine vs Real life and grass

Strength - Godot Engine
Size - Godot Engine
Speed - Godot Engine
IQ - Godot Engine
BIQ - Godot Engine
Combat - Godot Engine
Close combat - Godot Engine
Abilities - Godot Engine
Durability - Godot Engine
Power - Godot Engine
Weapons - Godot Engine
Agility - Godot Engine
Strategy - Godot Engine
Feats - Godot Engine
Protection - Godot Engine
Reaction - Godot Engine
Experience - Godot Engine
Offence - Godot Engine
Defence - Godot Engine
Stamina - Godot Engine
Hax - Godot Engine
Skills - Godot Engine
Endurance - Godot Engine
Legendary - Nobody
1on1 - Godot Engine
Overall - Godot Engine
Winner - Godot Engine

Godot stats:

STR – 20
DEX – 20
CON– 20
INT – 20
WIS– 20
CHA– 20

Ral life and grass stats:

STR – 0
DEX – 0
CON– 0
INT – 0
WIS– 0
CHA– 0

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.