This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Here is my enemy code.
Enemy Stops but doesn't move again:

extends KinematicBody

export var SPEED = 3
var target = null
var nav: Navigation = null
var vel = Vector3()

func _physics_process(delta):

    if target == null:
        return

    look_at(target.global_transform.origin, Vector3.UP)

    var path = get_to_path(target.global_transform.origin)
    if path.size() > 0:
        move_along_path(path)

func get_to_path(target):
    return nav.get_simple_path(global_transform.origin, target)

func move_along_path(path):
    if path.size() <= 0:
        return

    path.remove(0)

    var target = path[0]

    if global_transform.origin.distance_to(target) < 0.1:
        path.remove(0)

    vel = (target - translation).normalized() * SPEED

    vel = move_and_slide(vel)

func set_target(target):
    self.target = target

func set_nav(nav):
    self.nav = nav

func stop():
    SPEED = 0

func move():
    SPEED = 3


func _on_Area_area_entered(area):
    if area.is_in_group("STOPPER"):
        stop()
    else:
        vel = (target - translation).normalized() * 3extends KinematicBody

export var SPEED = 3
var target = null
var nav: Navigation = null
var vel = Vector3()

func _physics_process(delta):

    if target == null:
        return

    look_at(target.global_transform.origin, Vector3.UP)

    var path = get_to_path(target.global_transform.origin)
    if path.size() > 0:
        move_along_path(path)

func get_to_path(target):
    return nav.get_simple_path(global_transform.origin, target)

func move_along_path(path):
    if path.size() <= 0:
        return

    path.remove(0)

    var target = path[0]

    if global_transform.origin.distance_to(target) < 0.1:
        path.remove(0)

    vel = (target - translation).normalized() * SPEED

    vel = move_and_slide(vel)

func set_target(target):
    self.target = target

func set_nav(nav):
    self.nav = nav

func stop():
    SPEED = 0

func move():
    SPEED = 3


func _on_Area_area_entered(area):
    if area.is_in_group("STOPPER"):
        stop()
    else:
        vel = (target - translation).normalized() * 3
Godot version 3.5
in Engine by (22 points)
edited by

1 Answer

+1 vote
Best answer

What you want is a boolean guard. So where you have

func _on_Area_area_entered(area):
    if area.is_in_group("STOPPER"):
        stop()
    else:
        vel = (target - translation).normalized() * 3

what you want is instead to have a variable boolean which is true when it can move and false when it cannot move. The when it enters the area set it to false and have code in your _process() function to stop movement then attach a signal for body exited to this script from the area2d to turn the boolean to true again. What is happening at the moment is it is stopping but never seeing anything to tell it to move again.

by (3,328 points)
selected by

I just added this and it works!!!

on the top I did

var can_move = true

then i added this

func _on_Area_area_entered(area):
    if area.is_in_group("STOPPER"):
        can_move = false


func _on_Area_area_exited(area):
        if area.is_in_group("STOPPER"):
            can_move = true

func _process(delta):
    if can_move == true:
        SPEED = 3
    elif can_move == false:
        SPEED = 0

Perfect glad it all worked for you!

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.