Why is my enemy sprite moving in only one direction?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Abhishek_98

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?

:bust_in_silhouette: Reply From: gmaps

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?

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).

Abhishek_98 | 2019-10-10 08:27

You don’t do that in for loop. Whole _physics_process 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.

gmaps | 2019-10-10 08:51

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

Abhishek_98 | 2019-10-11 01:57

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)

gmaps | 2019-10-11 07:34

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

Abhishek_98 | 2019-10-11 07:55