Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | TechNick |
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
print state and patrol_points, or pause game/breakpoint and check them in remote view.
Inces | 2022-09-17 18:40