Same game object behaving strangely (SOLVED)

Hey there, guys!
I’m sorry to open a new topic but I couldn’t find anything related.
I got three objects that should behave in the same way. Just moving around with some delay and so but they don’t. Sometimes the object just takes a break(?) or don’t even move at all. I’m using the exactly same script for them (just a timer.delay + go.animate with some variables to calculate the next location). Crime scene footage:

(the player is the one affected by gravity btw)

Thanks!

Can you please share some code as well? Hard to tell what’s wrong from the video.

Sure!

the anomaly script (attached to the gameobject):
(I need to check on editor to activate the effect)

if self.movement then
			movement.initiate(go.get_position(), self.movement_delay, self.movement_range, self.movement_speed)
		end

then the main moviment.lua (a module):
(sorry my code is a mess)

function A.initiate(initial_pos, delay, range, speed)
    A.isMoving = false
    local delay = delay or A.delay
    local range = range or A.range
    local speed = speed or A.speed_mult

    math.randomseed(os.clock() * 10000000)

    timer.delay(
        delay,
        true,
        function()
            if not A.isMoving then
                A.isMoving = true
                local next_pos =
                    vmath.vector3(
                    math.random(initial_pos.x - range, initial_pos.x + range),
                    math.random(initial_pos.y - range, initial_pos.y + range),
                    0
                )

                local distance = vmath.vector3()
                distance.x = go.get_position().x - next_pos.x
                distance.y = go.get_position().y - next_pos.y

                local sc = math.sqrt(distance.x * distance.x + distance.y * distance.y)

                go.animate(
                    ".",
                    "position",
                    go.PLAYBACK_ONCE_FORWARD,
                    next_pos,
                    go.EASING_OUTELASTIC,
                    speed * sc,
                    0,
                    function()
                        A.isMoving = false
                    end
                )
            end
        end
    )
end

thanks!

What is the delay you are using on the timer? Could it be that the duration sometimes is longer than the timer delay in which case the next animation will be ignored since A.moving is still true.

PS There is a vmath.length() you could use to calculate distance

1 Like

Or could it be that the new random position is the same as the last random position?

1 Like

If they’re supposed to move independently, I’m not sure why they all rely on the same global variable ’A.isMoving’?

2 Likes

Ah, genius! That’s definitely a source of problems.

1 Like

Fixed! Thanks a lot! @britzl @Mathias_Westerdahl :raised_hands:

aaa

1 Like