0 votes

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)
asked Aug 10 in Engine by ThreeSpark (32 points)

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)

1 Answer

0 votes

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
answered Aug 11 by kidscancode (4,890 points)

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

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 How to use this Q&A? before posting your first questions.