0 votes

I keep changing a variable with a timer, but the changed variable doesn't affect my
physicsprocress(delta) function. Any idea why? The timer is working as the print function in this code works

extends Area2D

var speed=30
var dir=Vector2(0,0)

func _on_Timer_timeout():
    print("timeout")
    var randnum=rand_range(1,8)
    if(randnum==1):
        dir=Vector2(0,1)
    if(randnum==2):
        dir=Vector2(0,-1)
    if(randnum==3):
        dir=Vector2(1,0)
    if(randnum==4):
        dir=Vector2(-1,0)
    if(randnum==5):
        dir=Vector2(1,1)
    if(randnum==6):
        dir=Vector2(1,-1)
    if(randnum==7):
        dir=Vector2(-1,1)
    if(randnum==8):
        dir=Vector2(-1,-1)

func _physics_process(delta):
    translate(speed*dir)
asked Jan 15 in Engine by eclipse682 (12 points)

1 Answer

+3 votes

rand_range() returns a float in the range, so you're never going to have your random number be a whole number.

Use this instead to get an integer from 1 through 8:

var randnum = randi() % 8 + 1
answered Jan 15 by kidscancode (13,074 points)
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.