Enemy Not Following Player

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

So I have been having problems with my slime, so I decided to use rand_range to determine which player it should follow, and only follow. For some reason, the slime won’t move at all. Any suggestions? Here’s my code:

extends KinematicBody2D

onready var player = get_node("../Player")
onready var player2 = get_node("../Player2")
var velocity = Vector2()
var speed = 5000
enum {RIGHT, LEFT, UP, DOWN, IDLE_RIGHT, IDLE_LEFT, IDLE_UP, IDLE_DOWN}
var anim
var new_anim
var state
var min_distance = 4
var selection = rand_range(1, 2)

func change_state(new_state):
	state = new_state
	match state:
		RIGHT:
			new_anim = 'right'
		LEFT:
			new_anim = 'left'
		UP:
			new_anim = 'up'
		DOWN:
			new_anim = 'down'
		IDLE_RIGHT:
			new_anim = 'idle_right'
		IDLE_LEFT:
			new_anim = 'idle_left'
		IDLE_UP:
			new_anim = 'idle_up'
		IDLE_DOWN:
			new_anim = 'idle_down'

func _ready():
	change_state(IDLE_RIGHT)

func follow_player1():
	if player.position.x > position.x:
		velocity.x += speed
		change_state(RIGHT)
	if player.position.x < position.x:
		velocity.x -= speed
		change_state(LEFT)
	if player.position.y > position.y:
		velocity.y += speed
		change_state(DOWN)
	if player.position.y < position.y:
		velocity.y -= speed
		change_state(UP)

func follow_player2():
	if player2.position.x > position.x:
		velocity.x += speed
		change_state(RIGHT)
	if player2.position.x < position.x:
		velocity.x -= speed
		change_state(LEFT)
	if player2.position.y > position.y:
		velocity.y += speed
		change_state(DOWN)
	if player2.position.y < position.y:
		velocity.y -= speed
		change_state(UP)

func _process(delta):
	if selection == 1:
		follow_player1()
	if selection == 2:
		follow_player2()
	if new_anim != anim:
		anim = new_anim
		$AnimationPlayer.play(anim)
	

func _physics_process(delta):
	velocity = move_and_slide(velocity * delta)

Another comment: you don’t need two separate follow functions.

func follow(target):
    if target.position.x > position.x:
        velocity.x += speed
        change_state(RIGHT)
    if target.position.x < position.x:
        velocity.x -= speed
        change_state(LEFT)
    if target.position.y > position.y:
        velocity.y += speed
        change_state(DOWN)
    if target.position.y < position.y:
        velocity.y -= speed
        change_state(UP)

func _process(delta):
    if selection == 1:
        follow(player1)
    if selection == 2:
        follow(player2)

kidscancode | 2018-08-11 00:24

:bust_in_silhouette: Reply From: kidscancode

rand_range() produces a floating point number between the two given numbers, so selection is never going to be 1 or 2 but something like 1.2391.

If you want a random integer of either 1 or 2, try

var selection = randi() % 2 + 1

yeah… just fixed that a minute ago, actually! I decided to simply go for rand_range(1, 10) and use <= 5 and >=6

ThreeSpark | 2018-08-11 00:20