#Position resetting to 0

5 messages · Page 1 of 1 (latest)

fading plover
#

Testing out curves to create a bobbing motion after the node reaches it's target destination, but once the bobbing motion starts the position is reset to 0. I don't know how to stop this from happening. I have tried exchanging position.y for velocity and multiplying the position by direction, but while the current position is kept, the resulting motion looks like a hard bounce when reaching the lowest position. The code is pasted below. Is there an easier way to do this? Thanks in advance.

    if global_position != target.global_position && startup == true:
        var path_direction = target.global_position - global_position
        time_elapsed += (delta/ travel_time)
        position = position + path_direction * position_curve.sample(time_elapsed)
    else:
        startup = false
        if startup == false:
            time_elapsed += delta * dir
            if time_elapsed >= 1:
                dir = -dir
            elif time_elapsed <= 0:
                dir = -dir
            
            position.y = position_curve.sample(time_elapsed) * travel_time```
sinful pilot
#

Hi, I assume the position.y = position_curve.sample(time_elapsed) * travel_time
Is the bobbing motion?

fading plover
#

Yes, that is correct

sinful pilot
# fading plover Yes, that is correct

I think you could've done: position.y = final_position.y + position_curve.sample(time_elapsed) * travel_time

you could store the final_position as vector at the top

so like

var final_position = Vector2.ZERO

func _movement(delta):
if global_position != target.global_position && startup == true:
var path_direction = target.global_position - global_position
time_elapsed += (delta/ travel_time)
position = position + path_direction * position_curve.sample(time_elapsed)
final_position = position
else:
startup = false
if startup == false:
time_elapsed += delta * dir
if time_elapsed >=1 or time_elapsed <= 0:
dir = -dir

        position.y = final_position.y + position_curve.sample(time_elapsed) * travel_time
#

Btw you can just combine the two time_elapsed if conditions into 1 if since they both do the same thing, I did that in the updated code I provided, hope everything works out :)
You can also try using prints to visualise the value and see at what point it drops to 0