0 votes

Trying to get my enemy AI to kind of just simply walk randomly in a direction around an area every time the timer goes off. When they move though they move in the direction, but will shake violently while doing so.

Any pointers or tips would be helpful! I'm definitely missing something in my logic I just can't seem to figure out what...

func _process(delta):
    if(AI_STATE == AI_IDLE && can_Move ==true):
        AI_IDLE()
    if(AI_STATE == AI_CHASE && can_Move ==true):
        AI_CHASE()
    if(is_colliding()):
            slide()

func _on_move_Timer_timeout():
if(can_Move == false):
    can_Move = true
else:
    can_Move = false

func random():
    randomize()
    return randi()%2

func AI_IDLE():
    var atpos = Vector2(Vector2(random(),random()))
    atpos = atpos.normalized() * 2 
    move(atpos)
in Engine by (39 points)

Is this the kind of behavior you are trying to implement?

Random Movement

Yes, that is exactly what I'm trying to do! Any idea what I'm doing wrong? My KinematicBody2D shakes like crazy while it is in motion.

I altered the code a bit:

var direction = Vector2(0, 0)

func _process(delta):
    if(AI_STATE == AI_IDLE && can_Move ==true):
        AI_IDLE()
    if(AI_STATE == AI_CHASE && can_Move ==true):
        AI_CHASE()

func _on_move_Timer_timeout():
    if(can_Move == false):
        can_Move = true
        direction = Vector2(random(), random()).normalized() * 50
    else:
        can_Move = false

func random():
    randomize()
    return randi()%21 - 10 # range is -10 to 10

func AI_IDLE():
    move_and_slide(direction)

The most important change is that you should have a "direction" variable that gets updated only when the Timer timeouts. The code you posted looks like it updates the direction every time _process(...) is called, which could cause the jitters you experienced.

Thank you so much! This fixed the problem and I understand what the heck I was doing wrong!

Please log in or register to answer this question.

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.