![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | ThreeSpark |
So I made a selection variable that is rand_range(-10, 10) and if it is <=0 it follows player 1and if it is >= 1 it follows player 2. It was only following player 1, so I experimented a little and then it would only follow player 2. I put it back to the original code, and it only followed player 1. After some thinking, I realized that it was because the computer was only picking numbers less than or equal to five. Before I get this suggestion - yes, I have tried changing the maximum number. It always chooses to follow player 1. Any suggestions? Here is 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(-10, 10)
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(target):
if target.position.x > position.x && abs(target.position.x - position.x) > min_distance:
velocity.x += speed
change_state(RIGHT)
if target.position.x < position.x && abs(target.position.x - position.x) > min_distance:
velocity.x -= speed
change_state(LEFT)
if target.position.y > position.y && abs(target.position.y - position.y) > min_distance:
velocity.y += speed
change_state(DOWN)
if target.position.y < position.y && abs(target.position.y - position.y) > min_distance:
velocity.y -= speed
change_state(UP)
func _process(delta):
if selection >= 1:
follow(player2)
if selection <= 0:
follow(player)
if new_anim != anim:
anim = new_anim
$AnimationPlayer.play(anim)
func _physics_process(delta):
velocity = move_and_slide(velocity * delta)