0 votes

Hi, I am new to this engine and I am currently trying to make an enemy script.

enemy.gd:

 extends KinematicBody2D

const GRAVITY=20
const SPEED=50
var motion=Vector2()
var p=5

func _physics_process(delta):
    motion.y+=GRAVITY
    for p in range(6):
        if (p==2):
            motion.x=-SPEED
            $Sprite.play("walk")
            $Sprite.flip_h=true
        else:
            motion.x=SPEED
            $Sprite.play("walk")
            $Sprite.flip_h=false
    motion=move_and_slide(motion)
    pass

When I run the code, the enemy sprite starts moving towards the right direction and keeps on moving. It doesn't change direction. What am I doing wrong here?

in Engine by (22 points)

1 Answer

0 votes
Best answer

Because you iterate from 0 to 5. And the last iteration in the for loop is p == 5, which is not same as 2 and it goes into else statement motion.x = SPEED.

What exactly are you trying to achieve here? What is p?

by (713 points)
selected by

I am trying to move the enemy Sprite both sideways ( right and left). p is a counter which is used to move the Sprite in a particular direction (Like when p==2, then the enemy will move left, otherwise right).

You don't do that in for loop. Whole physicsprocess function is called 60 times per second by default. So all you need to do is something like this:

var p = 0
func _physics_process(delta):
    motion.y+=GRAVITY
    if (p==2):
        motion.x=-SPEED
        $Sprite.play("walk")
        $Sprite.flip_h=true
    else:
        motion.x=SPEED
        $Sprite.play("walk")
        $Sprite.flip_h=false
    p = (p + 1) % 6
    motion=move_and_slide(motion)

It will move the sprite two frames to the right, one frame to the left and then 3 frames to the right again. And repeat the whole cycle.

Tried it, its not working. The Sprite moves to the right and the left at the same time and as a result, caught in an infinite loop

I thought you wanted that, enemy moving in infinte loop based on your comments.. How does something move to the left and right at the same time? If you want it to move left for n steps and right for n steps do this:

And if you don't want infinite loop, just set the state to "STOP" if you want to change the number of states just set the p.

var p = 0
var direction = "LEFT" # enum RIGHT/LEFT/STOP

func _physics_process(delta):
    motion.y += GRAVITY
    if direction == "LEFT":
        motion.x = -SPEED
    elif direction == "RIGHT":
        motion.x = SPEED

    p += 1
    if p % 6 == 0:
        # After 6 movements set a new direction
        p = 0
        if direction == "RIGHT":
            direction = "LEFT"
        if direction == "LEFT":
            direction = "RIGHT


    motion = move_and_slide(motion)

Well I've used RayCast2D node and have somehow managed to move the enemy Sprite left or right based on the collision encountered. One thing I had done wrong is that I had modified the motion.x value under the is_on_wall() statement, and as a result the Sprite kept moving left and right at the same time continually. Now that I've fixed that, the enemy Sprite does move towards a particular direction until a ledge is encountered. But when it comes to a upward slope, the Sprite climbs half of it and changes direction. Please refer to my latest question about this issue. I have shared the code there

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.