0 votes

Hi!
I am currently working on a 2D bee game, where the players has to escape an enemy wasp. Before encountering the player, I want the wasp to patrol around the map. Therefore I have outlined a patrol_path using Path2D-Node. Yet upon starting the game, the wasp decides to fly another way (i guess to (0,0)).

I am following the tutorials by KidsCanCode.
https://kidscancode.org/godot_recipes/ai/path_follow/

extends KinematicBody2D

enum States {PATROL,ATTACK,CHASE}

var states=States.PATROL

export (NodePath) var patrol_path
var patrol_points
var patrol_index=0
export var patrol_speed=50

export var chase_speed = 100
var velocity=Vector2.ZERO 
var player=null



onready var animated_wasp: AnimationPlayer = get_node("AnimationPlayer")


func _ready():
    if patrol_path:
        patrol_points=get_node(patrol_path).curve.get_baked_points()

func _physics_process(delta):
    var velocity =Vector2.ZERO
    choose_action()


func choose_action():
    match states:

        States.PATROL:
            if !patrol_path:
                return
            var target = patrol_points[patrol_index]
            if position.distance_to(target)<1:
                patrol_index=wrapi(patrol_index+1,0,patrol_points.size())
                target=patrol_points[patrol_index]
            velocity=(target-position).normalized()*patrol_speed
            if velocity.x>0:
                animated_wasp.play("Wasp_Flight")
                $Wasp.set_flip_h(false)
            else:
                animated_wasp.play("Wasp_Flight")
                $Wasp.set_flip_h(true)
            velocity=move_and_slide(velocity)


        States.CHASE:
            if player:
                velocity=(player.position-position).normalized()*chase_speed

                if velocity.x>0:
                    animated_wasp.play("Wasp_Flight")
                    $Wasp.set_flip_h(false)
                else:
                    animated_wasp.play("Wasp_Flight")
                    $Wasp.set_flip_h(true)
            velocity=move_and_slide(velocity)

        States.ATTACK:
                pass



    pass


func _on_AttackRadius_body_entered(body):
    if body.get_name() =="Player":
        player=body
        states=States.ATTACK

func _on_AttackRadius_body_exited(body):
    if body.get_name()=="Player":
        states=States.CHASE

func _on_Detect_Radius_body_entered(body):
    if body.get_name()=="Player":
        player=body
        print("Playerentered")
        states=States.CHASE

func _on_Detect_Radius_body_exited(body):
    pass

func _on_Chase_Radius_body_exited(body):
    if body.get_name()=="Player":
        states=States.PATROL
Godot version 3.4.2.stable
in Engine by (15 points)

print state and patrol_points, or pause game/breakpoint and check them in remote view.

1 Answer

+1 vote
Best answer

At quick glance logic seems to be good. One thing to note is that Path2D returns points in local space. If you have moved, rotated or scaled the Path2D node then points don't match up with what you are seeing on screen.
You can adjust for this by transforming the points into global space:

var target_global = get_node(patrol_path).to_global(target)

Another possible pain point can emerge if wasp overshoots the target and ends up circling around it. If this does happen, raise the distance check to something like 10.

by (1,100 points)
selected by

Thank you very much! Thats was exactly the point. I changed all target coordinates to global, so now the problem is solved.

Glad to hear you could solve it! Don't forget to mark the question solved, so someone with a similiar problem can find it faster :)

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 Frequently asked questions and 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 [email protected] with your username.