Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | drorya |
Hi everyone
I’m currently making a 2D stealth game, including rooms, furniture(TileMap) and guards (KinematicBody2D).
I’m using Navigation2D, and get_simple_path to make the guards patrol around, but when they collide with any furniture- they get stuck.
I thought about a few ways to fix this, but the easiest one yet is to make the guards treat the furniture as walls, and use is_on_wall statement.
how do I do that?
And if you have a better idea, I would love to hear it, beacause I still havn’t figured out how to walk around the furniture. The best idea I’ve got is to make the guards choose a different destination.
Thank you all in advanced!
I think you cannot use isonwall because it determines something as a wall only for two directions while a furniture can be touch in any direction.
What are your using for ground?
Traumaticbean | 2020-05-03 04:04
Sorry it took so long for me to reply.
For the guard I’m using motion as Vector2, and move_and_slide().
here’s the script for the guard:
{
extends "res://Script/Character/NPCs/PlayerDetection.gd"
# Inheriting the following:
#const SPEED = 50
#const MAX_SPEED = 300
#const FRICTION = 0.25 # the time to get from MAX_SPEED to 0.
onready var navigation = get_tree().get_root().find_node("Navigation2D", true, false)
onready var destinations = navigation.get_node("Destinations")
var motion
var possible_destinations
var path =[]
export var minimum_arrival_distance = 5
export var walk_speed = 0.5
func _ready():
randomize()
possible_destinations = destinations.get_children()
make_path()
func _physics_process(delta):
navigate()
func make_path():
var new_destination = possible_destinations[randi() % possible_destinations.size() -1]
path = navigation.get_simple_path(global_position , new_destination.global_position , false)
func navigate():
var distance_to_destination = global_position.distance_to(path[0])
if distance_to_destination > minimum_arrival_distance:
move()
else:
update_path()
func move():
look_at(path[0])
motion = (path[0] - global_position).normalized() * (MAX_SPEED * walk_speed)
move_and_slide(motion)
if is_on_wall():
rotation_degrees += 90
make_path()
func update_path():
if path.size() == 1 :
if $Timer.is_stopped():
$Timer.start()
else:
path.remove(0)
func _on_Timer_timeout():
make_path()
}
drorya | 2020-05-13 05:49