Ai
Ai
3 ai
1.範圍內行走與追蹤
extends CharacterBody2D
class_name gearteethenemy
var health_max = 80
var health_min = 0
func _process(delta):
if !is_on_floor():
velocity.y += gravity * delta
velocity.x = 0
move(delta)
handle_animation()
move_and_slide()
func move(delta):
if !dead:
if !is_chase:
velocity += dir * speed * delta
elif is_chase and !taking_damage:
var dir_to_player = position.direction_to(player.global_position)
* speed
velocity.x = dir_to_player.x
dir.x = abs(velocity.x) / velocity.x
is_roaming = true
elif dead:
velocity.x = 0
func handle_animation():
var anim_sprite = $AnimatedSprite2D
if !dead and !taking_damage and !is_attacking:
anim_sprite.play("walk")
if dir.x ==-1:
anim_sprite.flip_h = true
elif dir.x == 1:
anim_sprite.flip_h = false
elif !dead and taking_damage and!is_attacking:
anim_sprite.play("hurt")
await get_tree().create_timer(0.8).timeout
taking_damage = false
elif dead and is_roaming:
is_roaming = false
anim_sprite.play("death")
await get_tree().create_timer(1.0).timeout
handle_death()
func handle_death():
self.queue_free()
func _on_timer_timeout():
$Timer.wait_time = choose([1.5, 2.0, 2.5])
if !is_chase:
dir = choose([Vector2.RIGHT, Vector2.LEFT])
velocity.x = 0
func choose(array):
array.shuffle()
return array.front()
2.pathfinding 無法偵測上方玩家
extends CharacterBody2D
if !is_on_floor():
velocity.y += gravity * delta
velocity.x = 0
move_and_slide()
# Handle jump.
func makepath() -> void:
nav_agent.target_position = player.global_position
3.大地圖追蹤法,玩家上方 area_2d+pathfinding
4.y 值 x 值 檢定法
extends CharacterBody2D
class_name snaker
var health_max = 80
var health_min = 0
handle_animation()
move_and_slide()
func move(delta):
velocity.x = dir.x * speed
if $RayCast2D.is_colliding():
var collider = $RayCast2D.get_collider()
if collider.is_in_group("wall"):
print("waall")
dir.x = 1
elif $RayCast2D2.is_colliding():
var collider = $RayCast2D2.get_collider()
if collider.is_in_group("wall"):
print("waall2")
dir.x = -1
func handle_animation():
var anim_sprite = $AnimatedSprite2D
if !dead and !taking_damage and !is_attacking:
anim_sprite.play("walk")
if dir.x == 1:
anim_sprite.flip_h = true
elif dir.x == -1:
anim_sprite.flip_h = false
elif !dead and taking_damage and!is_attacking:
anim_sprite.play("hurt")
await get_tree().create_timer(0.8).timeout
taking_damage = false
elif dead and is_roaming:
is_roaming = false
anim_sprite.play("death")
await get_tree().create_timer(1.0).timeout
func path_finding_chase():
var dir = to_local(nav_agent.get_next_path_position()).normalized()
velocity.x = dir.x * speed